我正在开发一个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对象始终使用null / default值填充。这应该相当简单,因为其他4种表格在不到一天的时间内完成。
我检查了标准内容,比如将Controller变量命名为独特的内容,并确保我的属性中没有包含类名中的关键字。所有这些东西都会检查出来,但我仍然坚持同样的问题!
答案 0 :(得分:2)
这里的主要问题是,您发布的值是NcProgrammingIssues
的前缀。这是因为你正在做@Html.EditorFor(model => model.NcProgrammingIssues.Employee)
。
如果您花一点时间通过浏览器网络标签查看发布的值,您就会明白我在说什么。
对此的解决应该相当容易:告诉Action
它应该期望前缀。
public ActionResult Create([Bind(Prefix="NcProgrammingIssues")]NcProgrammingIssue value){...}
希望这有帮助
编辑:关于模型绑定器如何工作的小注释:如果它注意到你的方法参数(在这种情况下是value
)与前缀具有相同的名称,它也会自动绑定它。所以在这种情况下,你理论上只需将value
重命名为NcProgrammingIssues