選擇標記究竟應該如何映射到MVC模型變量,我嘗試了所有我可能想到的方法,但是每次檢查控制器上的post方法時,變量假設綁定到下拉列表返回null。HTML下拉列表未映射到MVC模型
查看:
<script type="text/javascript">
var counter = 1;
$('#btnAddLocation').click(function (e) {
e.preventDefault();
$("#tblAddLocation").find('tbody')
.append($('<tr>')
.append($('<td>')
.append($('@Html.LabelFor(m => m.Location)')
)
)
.append($('<td>')
.append($('<select name=Location id=Location' + counter++ + ' class=gradientTextbox>')
)
)
.append($('<td>')
.append($('@Html.LabelFor(m => m.Description)')
)
)
.append($('<td>')
.append($('@Html.EditorFor(m => m.Description)')
)
)
);
$.getJSON("/Locations/GetAllLocations", function (data) {
var items = "<option selected></option>";
$.each(data, function (i, item) {
items += "<option value='" + item.Value + "'>" + item.Text + "</option>";
});
$("#Location" + (counter - 1)).html(items);
});
});
</script>
控制器:
[HttpPost]
public ActionResult ActionName(MyModel model, List<string> Location, List<string> Description) {
return Json(new { success = true });
}
在控制器代碼說明變量工作正常,但在列表的位置返回正確的計數,但所有項目爲空。
有關如何處理此問題的任何建議?
試過了,沒有工作。位置仍然返回爲空。 – theStig