检查表单中与模型绑定的字段名称是否正确,确认表单提交时是否包含对应字段的值。若确定绑定和提交无误,可能是因为模型不存在,或者在表单中没有正确地绑定模型。以下为可能的解决代码示例:
在模型中定义与表单中绑定的字段名称:
public class Person { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } }
将模型绑定至表单:
@model Person @using (Html.BeginForm("actionName", "controllerName", FormMethod.Post)) { @Html.LabelFor(m => m.Name) @Html.TextBoxFor(m => m.Name) @Html.LabelFor(m => m.Age) @Html.TextBoxFor(m => m.Age) }
在控制器中接收并处理数据:
[HttpPost] public ActionResult actionName(Person person) { if (person == null) { // 处理模型为空的情况 } else { // 处理正常情况 } }
上一篇:表单提交时,模型未被赋值。