Search code examples
javascriptrazordropdownlistfor

Returning ViewBag inside @Html.DropDownListFor inside single quotes


I have a DropDownListFor working this way:

        <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 have a javascript function that creates a container and fields. For this function, I pass the whole container between single quotes this way:

    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, and my problem, is when I try to pass a ViewBag (from the first idea) to a single quote. I am trying 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 with: @ViewBag.Currency

And, with: '@ViewBag.Currency'

Nothing is working. For the 1st option, it gives the error: "ReferenceError: DynamicText is not defined". The 2nd idea gives me the same error. When I try to put the single quotes, it does not even compile and gives me the error: struct System.Char - Represents a character as a UTF-16 code unit. Too many characters in character literal

Finally, I tried this way: I also tried like this (idea from):

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 way compiles but the pages does load at all.

Any ideas?


Solution

  • This is what I think you should do,

    Instead of using single quotes, use `. I don't really remember what it is called again. That way you can write your html without thinking of single or double quotes issues

    Also I don't know if it was a typo you have id=CurrencyDataBlock in your HTML

    var part3 = `<div class="form-horizontal" id="CurrencyDataBlock">
                    @Html.DropDownListFor(model => model.headline, ViewBag.Currency as SelectList, "--Select Currency--", new { @class = "btn btn-default col-xs-12 col-xs-offset-0 col-sm-2" })
                </div>`;
    
    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>`;
    

    Also when you want to pass lists consider using @Html.Raw("your list or other variables here")