【问题标题】:Inserting DropDownList helper from javascript从 javascript 中插入 DropDownList 助手
【发布时间】:2017-12-12 12:21:32
【问题描述】:

这是我的代码

<a class="fa fa-plus fa-lg" onclick="addFilterItem()"></a>

...

<div id="someId"></div>

...

//in a script tag
function addFilterItem() {
    var container = $("#someId");
    var helper = '@Html.DropDownList("something", new SelectList(ViewBag.list), new { @class = "form-control" })';
    container.append("<div>" + helper + "</div>");
    ..
}

对我来说奇怪的是,我首先用 TextBox 进行了这样的测试

var helper = '@Html.TextBox("something")';

而且它有效。那么为什么它也不能与 DropDownList 一起使用呢?还有哪些替代方案?

... 没关系,我明白了。

【问题讨论】:

    标签: javascript model-view-controller asp.net-core html-helper


    【解决方案1】:

    显然,DropDownList 帮助器在多行上返回一个字符串(我在浏览器中使用了查看源代码),使用 '' 或 "" 只会给你一个错误(字符串未完成)。在查看了在 javascript 中在多行上编写字符串的方法之后,我发现了 ``.反引号。所以像这样写它为我解决了它

    var helper = `@Html.DropDownList("something", new SelectList(ViewBag.list), new { @class = "form-control" })`;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-10
      • 2016-05-10
      • 2019-02-05
      • 1970-01-01
      • 2013-09-17
      相关资源
      最近更新 更多