I'm currently working with a DropDownListFor that is set up like this:
<div class="form-horizontal" id=CurrencyDataBlock>
@Html.DropDownListFor(model => model.Code, ViewBag.Currency as SelectList, "--Select Currency--", new { @class = "btn btn-default col-xs-12 col-xs-offset-0 col-sm-2" })
</div>
In my JavaScript function, I am creating a container and fields. To pass the entire container within single quotes, I do it like this:
var part1 = '<br><br><hr style="height:1px;border:none;color:#333;background-color:#333;" /><div><input type="button" class="btn btn-default" onclick="ReTextBox(this)" value="Remove"/></div><textarea rows="10" cols="100" id="Text" name="dyntxt" style="width: 550px; height: 125px; margin-left: auto;"></textarea>'
However, I encounter an issue when trying to pass a ViewBag (from the initial setup) into single quotes. I attempted it like this:
var part3 ='<div class="form-horizontal" id=CurrencyDataBlock>@Html.DropDownListFor(model => model.Code, ViewBag.Currency as SelectList, "--Select Currency--", new { @class = "btn btn-default col-xs-12 col-xs-offset-0 col-sm-2" })</div>'
I also tried alternatives like: @ViewBag.Currency
And: '@ViewBag.Currency'
But nothing seems to be successful. The first option results in an error: "ReferenceError: DynamicText is not defined". The second attempt produces the same error. When attempting to use single quotes directly, it doesn't even compile and shows an error like: struct System.Char - Represents a character as a UTF-16 code unit. Too many characters in character literal
Finally, I experimented with this approach: I also tried this (idea):
var part3 ='<div class="form-horizontal" id=CurrencyDataBlock>@Html.DropDownListFor(model => model.Code, ViewBag.Currency.Replace("\\", "\\\\").Replace("'", "\\'") as SelectList, "--Select Currency--", new { @class = "btn btn-default col-xs-12 col-xs-offset-0 col-sm-2" })</div>'
This method compiles without errors, but the page fails to load.
Any suggestions or ideas?