插入来自 javascript 的 DropDownList 助手

Inserting DropDownList helper from javascript

这是我的代码

<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 一起使用呢?还有哪些替代方案?

... 没关系,我知道了。

很明显 DropDownList 助手 returns 是多行的字符串(我在浏览器中使用了查看源代码)并且使用 '' 或 "" 只会给你一个错误(字符串未完成)。在查看了在 javascript 中的多行上写入字符串的方法后,我发现了``。反引号。所以这样写对我来说是固定的

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