ASP .NET MVC - 创建部分视图仅适用于“编辑”; actionresult 而不是“创建”
我创建了一个部分视图,供模型的“编辑”视图使用。我可以成功编辑记录,但在“创建”视图中使用部分视图时,出现空引用异常。
这是我的部分视图:
@model MvcA.Models.Reason
@Html.LabelFor(model => model.reason)
@Html.EditorFor(model => model.reason)
@Html.LabelFor(model => model.Contract)
@Html.DropDownList("ContractId",
new SelectList(ViewBag.Contract as System.Collections.IEnumerable,
"ContractId","Name",Model.ContractID));
POST ActionResult:
[HttpPost]
public ActionResult Create(Reason reason)
{
if (ModelState.IsValid)
{
db.Reason.Add(reason);
db.SaveChanges();
return RedirectToAction("Index");
}
//invalid ...
GET Create:
public ActionResult Create()
{
ViewBag.Contract = db.Contract.OrderBy(g => g.Name).ToList();
var reason = new Reason();
return View(reason);
}
输入/选择有效值后,表单提交将导致 Visual Studio 退出到部分视图中找到的“DropDownList”,并显示“NullReferenceException 未处理”。
如何确定导致 null 错误的原因? (我是 MVC 新手)
更新:该错误似乎与我的控制器中的 [HttpPost] Create 方法有关。我使用与模型中的字段之一相同的名称来命名输入类...这似乎因空引用异常而破坏了程序。
I have created a partial view for use by by the 'Edit' view of my model. I can successfully edit records but when using the partial view for my 'Create' view I get a null reference exception.
This is my partial view:
@model MvcA.Models.Reason
@Html.LabelFor(model => model.reason)
@Html.EditorFor(model => model.reason)
@Html.LabelFor(model => model.Contract)
@Html.DropDownList("ContractId",
new SelectList(ViewBag.Contract as System.Collections.IEnumerable,
"ContractId","Name",Model.ContractID));
And POST ActionResult:
[HttpPost]
public ActionResult Create(Reason reason)
{
if (ModelState.IsValid)
{
db.Reason.Add(reason);
db.SaveChanges();
return RedirectToAction("Index");
}
//invalid ...
The GET Create:
public ActionResult Create()
{
ViewBag.Contract = db.Contract.OrderBy(g => g.Name).ToList();
var reason = new Reason();
return View(reason);
}
Upon entering/selecting valid values the form submit will result in Visual Studio exiting out to the 'DropDownList' found in the partial view with a 'NullReferenceException was unhandled'.
How do I determine what is causing the null error? (I'm new to MVC)
UPDATE: The error appears to be related to the [HttpPost] Create method in my controller. I was naming the input class using the same name as one of the fields in the model...this appears to have broken the program with the null reference exception.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
当您发布到创建操作时,当您收到异常时,模型是有效还是无效?如果它无效,可能是因为您返回一个视图来显示带有验证的表单,但缺少该视图的一些要求(例如 ViewBag.Contract 正在填充)。如果完整显示这两个创建操作,将更容易验证这一点。
When you post to the create action is the model valid or invalid when you get the exception? If it is invalid, it is likely because you're returning a view to show the form with validation but are missing some of the requirements for that view (like however ViewBag.Contract is getting populated). If you show both Create actions in full it will be easier to verify this.
当您渲染创建的部分视图时,请尝试:
When you render your create partial view, try :