ASP .NET MVC - 创建部分视图仅适用于“编辑”; actionresult 而不是“创建”

发布于 2024-10-16 12:01:25 字数 1246 浏览 3 评论 0原文

我创建了一个部分视图,供​​模型的“编辑”视图使用。我可以成功编辑记录,但在“创建”视图中使用部分视图时,出现空引用异常。

这是我的部分视图:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

锦欢 2024-10-23 12:01:25

当您发布到创建操作时,当您收到异常时,模型是有效还是无效?如果它无效,可能是因为您返回一个视图来显示带有验证的表单,但缺少该视图的一些要求(例如 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.

你曾走过我的故事 2024-10-23 12:01:25

当您渲染创建的部分视图时,请尝试:

<% Html.RenderPartial("YouPartialViewName", new Reason()); %>

When you render your create partial view, try :

<% Html.RenderPartial("YouPartialViewName", new Reason()); %>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文