【发布时间】:2019-08-01 03:31:31
【问题描述】:
在我的 cshtml 和我的脚本中,我有以下有效的循环:
for (var i = 0; i < xValue.length; i++) {
var obj = xValue[i];
if (obj.NameOfViewData.slice(0, obj.NameOfViewData.indexOf("_")) == $("#SeriesTypeId option:selected").text().replace(" ", "")) {
console.log(obj.NameOfViewData);
console.log(obj.NameOfViewData.slice(0, obj.NameOfViewData.indexOf("_")));
var iname = "Pt_ReturnType";
$(container).append(`<div class="input" id=td` + iCnt + `_` + i + ` ` +
`value="Drops Element ` + iCnt + `_` + i + `">@Html.DropDownListFor(model => model.TagValueName, ViewData["Pt_ReturnType"] as SelectList, "--" + "testing" + "--")</div>`);
}
}
但是,当我尝试更改 ViewData 以接收具有相同名称的字符串 var 时,它不起作用。我尝试了什么(来自here 的想法):
$(container).append(`<div class="input" id=td` + iCnt + `_` + i + ` ` +
`value="Drops Element ` + iCnt + `_` + i + `">@Html.DropDownListFor(model => model.TagValueName, ViewData[`${iname}`] as SelectList, "--" + "testing" + "--")</div>`);
(这个“破坏”了 HTML 页面)
我也试过了:
$(container).append(`<div class="input" id=td` + iCnt + `_` + i + ` ` +
`value="Drops Element ` + iCnt + `_` + i + `">@Html.DropDownListFor(model => model.TagValueName, ViewData[iname] as SelectList, "--" + "testing" + "--")</div>`);
(这个,它不识别“iname as variable”)
请注意我使用的是 ` 而不是 "。原因是在我之前问过 here 的问题中。
【问题讨论】:
-
不幸的是,这不是 Razor 语法的工作方式。 @Html 部分在服务器上进行评估,并且 ` 在那里无效。
标签: jquery razor dropdownlistfor