发布的模型始终为Null
本文关键字:Null 模型 | 更新日期: 2023-09-27 18:26:46
我正在处理一个MVC4应用程序,提交一份表格时遇到了困难。我已经有4个其他提交表单可以完美地工作,但这个表单根本不起作用。填写表单后,控制器中接收的对象总是用空值填充。
控制器:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(NcProgrammingIssue value)
{
if (ModelState.IsValid)
{
db.NcProgrammingIssues.Add(value);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(value);
}
型号:
public partial class NcProgrammingIssue
{
public int ID { get; set; }
public System.DateTime Date { get; set; }
public string Employee { get; set; }
public string JobNumber { get; set; }
public string PartNumber { get; set; }
public int OprSeq { get; set; }
public string ResourceDescription { get; set; }
public string EmployeeNum { get; set; }
public string Severity { get; set; }
public string LineNumbers { get; set; }
public string Comment { get; set; }
public string SuggestedChange { get; set; }
public string NcProgName { get; set; }
}
视图:
@model ShopConnectMVC.Models.ShopConnectEntities
@{
ViewBag.Title = "Create";
}
<h2>Create</h2>
@using (Html.BeginForm()) {
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)
<fieldset>
<legend>NcProgrammingIssue</legend>
<div class="editor-label">
@Html.LabelFor(model => model.NcProgrammingIssues.Date)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NcProgrammingIssues.Date)
@Html.ValidationMessageFor(model => model.NcProgrammingIssues.Date)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NcProgrammingIssues.Employee)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NcProgrammingIssues.Employee)
@Html.ValidationMessageFor(model => model.NcProgrammingIssues.Employee)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NcProgrammingIssues.JobNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NcProgrammingIssues.JobNumber)
@Html.ValidationMessageFor(model => model.NcProgrammingIssues.JobNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NcProgrammingIssues.PartNumber)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NcProgrammingIssues.PartNumber)
@Html.ValidationMessageFor(model => model.NcProgrammingIssues.PartNumber)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NcProgrammingIssues.OprSeq)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NcProgrammingIssues.OprSeq)
@Html.ValidationMessageFor(model => model.NcProgrammingIssues.OprSeq)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NcProgrammingIssues.ResourceDescription)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NcProgrammingIssues.ResourceDescription)
@Html.ValidationMessageFor(model => model.NcProgrammingIssues.ResourceDescription)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NcProgrammingIssues.EmployeeNum)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NcProgrammingIssues.EmployeeNum)
@Html.ValidationMessageFor(model => model.NcProgrammingIssues.EmployeeNum)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NcProgrammingIssues.NcProgName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NcProgrammingIssues.NcProgName)
@Html.ValidationMessageFor(model => model.NcProgrammingIssues.NcProgName)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NcProgrammingIssues.Severity)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NcProgrammingIssues.Severity)
@Html.ValidationMessageFor(model => model.NcProgrammingIssues.Severity)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NcProgrammingIssues.LineNumbers)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NcProgrammingIssues.LineNumbers)
@Html.ValidationMessageFor(model => model.NcProgrammingIssues.LineNumbers)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NcProgrammingIssues.Comment)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NcProgrammingIssues.Comment)
@Html.ValidationMessageFor(model => model.NcProgrammingIssues.Comment)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.NcProgrammingIssues.SuggestedChange)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.NcProgrammingIssues.SuggestedChange)
@Html.ValidationMessageFor(model => model.NcProgrammingIssues.SuggestedChange)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
这种观点只是标准的脚手架,但它拒绝起作用。传递给控制器的NcProgrammingIssue对象始终填充空值/默认值。这应该相当简单,因为其他4张表格在不到一天的时间内就完成了。
我检查了标准的东西,比如将Controller变量命名为唯一的,并确保我的属性中没有包含类名中的关键字。所有这些东西都检验出来了,但我仍然遇到了同样的问题!
这里的主要问题是您发布的值以NcProgrammingIssues
作为前缀。这是由于您正在执行@Html.EditorFor(model => model.NcProgrammingIssues.Employee)
。如果你花一分钟时间通过浏览器网络选项卡查看发布的值,你就会明白我在说什么。
这个问题的解决方案应该相当简单:告诉Action
它应该使用前缀。
public ActionResult Create([Bind(Prefix="NcProgrammingIssues")]NcProgrammingIssue value){...}
希望这能帮助
编辑:关于modelbinder如何工作的小注释:如果它注意到你的方法参数(在本例中为value
)与前缀同名,它也会自动绑定它。因此,在这种情况下,从理论上讲,可以通过将value
重命名为NcProgrammingIssues