Returning ViewBag inside @Html.DropDownListFor inside single quotes

落爺英雄遲暮 提交于 2019-12-13 03:46:05

问题


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?


回答1:


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")



来源:https://stackoverflow.com/questions/57227324/returning-viewbag-inside-html-dropdownlistfor-inside-single-quotes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!