MVC5 @Html.EditorFor: 第二个属性问题
MVC5 @Html.EditorFor: Second Attribute Problem
我对 MVC5 中的 @html.EditorFor 有点失望,在 "Create View"
基本上,我有一个下拉菜单,用户可以从中选择信息。在 Change 上,下拉列表的值被传递(通过 javascript)到相对 @Html.EditorFor,以便在提交视图时保存在 table 中。
这是我的 DropDown 视图代码(下拉列表本身由索引控制器填充,并且运行良好)
@Html.DropDownList("testList", null, "Select Delivery Unit", new { htmlAttributes = new { @class = "form-control" } })
这是我的 EditorFor 视图代码:
@Html.EditorFor(model => model.DeliveryUnitID, null, "myunit", new { htmlAttributes = new { @class = "form-control" } })
虽然 JavaScript 工作正常,但我也会包含该代码,以备不时之需:
<script type="text/javascript">
$(function () {
$("[name='testList']").change(function () {
$("#myunit").val($(this).val());
});
});
</script>
用户从 "testlist" 下拉列表中选择一个选项,然后该值将通过提供的 javascript 传递给 "myunit"。一切都很好。但是,当我保存数据时。 . .该字段始终为空。它没有捕获值。
我认为问题出在第二个属性 (null) 上。
我需要更改什么才能使其正常工作?
更新:这是创建视图控制器代码
public ActionResult Create()
{
List<SelectListItem> testList = db.ICS_Units.Select(x => new SelectListItem { Value = x.DeliveryUnitID.ToString(), Text = x.DeliveryUnit, Selected = false }).DistinctBy(p => p.Text).ToList();
ViewBag.testList = new SelectList(testList, "Value", "Text");
return View();
}
// POST: InternalOrders/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "TransID,SuppliesID,OriginalDate,TransType,LastUpdatedBy,Contact,OpenClosed,CurrentStatus,CurrentStatusDate,RequsitionNumber,PONumber,DeliveryMonth,DeliveryYear,UnitsOrdered,Emergency,Comments,DeliveryUnitID")] ICS_Transactions iCS_Transactions)
{
if (ModelState.IsValid)
{
db.ICS_Transactions.Add(iCS_Transactions);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(iCS_Transactions);
}
您将 HtmlFieldName 强加到您的字段编辑器这一事实更改了默认标记和您发布的数据。保留字段名称,而不是更新您的 jquery 以匹配模型字段名称:
$('#DeliveryUnitID').val($(this).val());
我对 MVC5 中的 @html.EditorFor 有点失望,在 "Create View"
基本上,我有一个下拉菜单,用户可以从中选择信息。在 Change 上,下拉列表的值被传递(通过 javascript)到相对 @Html.EditorFor,以便在提交视图时保存在 table 中。
这是我的 DropDown 视图代码(下拉列表本身由索引控制器填充,并且运行良好)
@Html.DropDownList("testList", null, "Select Delivery Unit", new { htmlAttributes = new { @class = "form-control" } })
这是我的 EditorFor 视图代码:
@Html.EditorFor(model => model.DeliveryUnitID, null, "myunit", new { htmlAttributes = new { @class = "form-control" } })
虽然 JavaScript 工作正常,但我也会包含该代码,以备不时之需:
<script type="text/javascript">
$(function () {
$("[name='testList']").change(function () {
$("#myunit").val($(this).val());
});
});
</script>
用户从 "testlist" 下拉列表中选择一个选项,然后该值将通过提供的 javascript 传递给 "myunit"。一切都很好。但是,当我保存数据时。 . .该字段始终为空。它没有捕获值。
我认为问题出在第二个属性 (null) 上。
我需要更改什么才能使其正常工作?
更新:这是创建视图控制器代码
public ActionResult Create()
{
List<SelectListItem> testList = db.ICS_Units.Select(x => new SelectListItem { Value = x.DeliveryUnitID.ToString(), Text = x.DeliveryUnit, Selected = false }).DistinctBy(p => p.Text).ToList();
ViewBag.testList = new SelectList(testList, "Value", "Text");
return View();
}
// POST: InternalOrders/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "TransID,SuppliesID,OriginalDate,TransType,LastUpdatedBy,Contact,OpenClosed,CurrentStatus,CurrentStatusDate,RequsitionNumber,PONumber,DeliveryMonth,DeliveryYear,UnitsOrdered,Emergency,Comments,DeliveryUnitID")] ICS_Transactions iCS_Transactions)
{
if (ModelState.IsValid)
{
db.ICS_Transactions.Add(iCS_Transactions);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(iCS_Transactions);
}
您将 HtmlFieldName 强加到您的字段编辑器这一事实更改了默认标记和您发布的数据。保留字段名称,而不是更新您的 jquery 以匹配模型字段名称:
$('#DeliveryUnitID').val($(this).val());