【问题标题】:Returning ViewBag inside @Html.DropDownListFor inside single quotes在单引号内返回 @Html.DropDownListFor 内的 ViewBag
【发布时间】:2019-07-26 21:45:51
【问题描述】:

我有一个 DropDownListFor 这种方式工作:

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

我有一个创建容器和字段的 javascript 函数。对于这个函数,我以这种方式在单引号之间传递整个容器:

    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>'

但是,我的问题是,当我尝试将 ViewBag(从第一个想法)传递给单引号时。我正在尝试这样:

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>'

我也尝试过: @ViewBag.Currency

并且,与: '@ViewBag.Currency'

没有任何工作。对于第一个选项,它给出了错误: “ReferenceError:DynamicText 未定义”。第二个想法给了我同样的错误。当我尝试加上单引号时,它甚至没有编译并给我错误: struct System.Char - 将字符表示为 UTF-16 代码单元。字符文字中的字符太多

最后,我尝试了这种方式: 我也试过这样(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>'

这种方式可以编译,但页面确实会加载。

有什么想法吗?

【问题讨论】:

    标签: javascript razor dropdownlistfor


    【解决方案1】:

    这是我认为你应该做的,

    不要使用单引号,而是使用 `。我真的不记得它又叫什么了。这样您就可以编写 html 而无需考虑单引号或双引号问题

    我也不知道是不是你的 HTML 中有 id=CurrencyDataBlock 的错字

    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>`;
    

    此外,当您想传递列表时,请考虑使用@Html.Raw("your list or other variables here")

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-13
      • 2013-05-13
      • 1970-01-01
      • 2016-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多