ASP.NET MVC应用程序抛出system.nullreferenceexception - 模型绑定到列表

时间:2015-06-01 16:58:02

标签: c# asp.net asp.net-mvc nullreferenceexception model-binding

我有一个asp.net应用程序,它有一个用户可以修改和保存的索引视图。但是,当我单击保存按钮时,我收到错误:

  Object reference not set to an instance of an object.
System.NullReferenceException: Object reference not set to an instance of an object.

我已经将我的索引页面设置为具有多个复选框的表单,在用户检查了他们需要的内容并点击保存后,它会调用我的保存功能。

Index.cshtml

<input type="button" onclick="$('#doctorform').submit();" value="Save" />

@using (Html.BeginForm("Index", "drnos", FormMethod.Post, new { id = "doctorform" }))
{


 for (int i = 0; i < Model.Count; i++)
 {

        <tr>
            <td>
                @Html.Hidden("drnos[" + @i + "].RVH_ID_", Model[i].AdmPriv)
                @Model[@i].RVH_ID_

            </td>
            <td>
                @Html.Hidden("drnos[" + @i + "].Last_Name", Model[i].AdmPriv)
                @Model[@i].Last_Name
            </td>
            <td>
                @Html.Hidden("drnos[" + @i + "].First_Name", Model[i].AdmPriv)
                @Model[@i].First_Name
            </td>
            <td>
                @Html.Hidden("drnos[" + @i + "].Middle_Name", Model[i].AdmPriv)
                @Model[@i].Middle_Name
            </td>
            <td>
                @Html.Hidden("drnos[" + @i + "].Degree1", Model[i].AdmPriv)
                @Model[@i].Degree1
            </td>
            <td>
                @Html.Hidden("drnos[" + @i + "].Group", Model[i].AdmPriv)
                @Model[@i].Group
            </td>
            <td>
                @Html.CheckBox("drnos[" + @i + "].AdmPriv", Model[i].AdmPriv)
            </td>
            <td>
                @Html.CheckBox("drnos[" + @i + "].QCPR", Model[i].QCPR)
            </td>
            <td>
                @Html.CheckBox("drnos[" + @i + "].Keane", Model[i].Keane)

            </td>
            <td>
                @Html.CheckBox("drnos[" + @i + "].Orsos", Model[i].Orsos)

            </td>
            <td>
                @Html.CheckBox("drnos[" + @i + "].Soft", Model[i].Soft)

            </td>
            <td>
                @Html.CheckBox("drnos[" + @i + "].C3M", Model[i].C3M)
            </td>


            <td>
                @Html.ActionLink("Edit", "Edit", new { id = Model[@i].RVH_ID_ })
                @Html.ActionLink("Details", "Details", new { id = Model[@i].RVH_ID_ })

            </td>
        </tr>

        }

        }

drnosController.cs

 [HttpPost]
    public ActionResult Index(List<Doctor> drnos)
    {

        System.Diagnostics.Debug.WriteLine("Save Called");
        DrNOSv2Entities db = new DrNOSv2Entities();
        foreach (Doctor doc in drnos)
        {
            Doctor existing = db.Doctors.Find(doc.RVH_ID_);
            existing.AdmPriv = doc.AdmPriv;
            existing.QCPR = doc.QCPR;
            existing.Keane = doc.Keane;
            existing.Orsos = doc.Orsos;
            existing.Soft = doc.Soft;
            existing.C3M = doc.C3M;
        }
        db.SaveChanges();
        return View();
    }

模型

namespace drnosv6.Models
{
    using System;
    using System.Collections.Generic;

public partial class Doctor
{
    public int RVH_ID_ { get; set; }
    public string NPI { get; set; }
    public bool On_Staff { get; set; }
    public bool AdmPriv { get; set; }
    public bool Inactive { get; set; }
    public string License_Verification_Date { get; set; }
    public string Last_Name { get; set; }
    public string First_Name { get; set; }
    public string Middle_Name { get; set; }
    public string Group { get; set; }
    public string Address { get; set; }
    public string City { get; set; }
    public string Zip { get; set; }
    public string State { get; set; }
    public string Telephone { get; set; }
    public string Fax { get; set; }
    public string Phys_Spclty { get; set; }
    public string License_ { get; set; }
    public string UPIN { get; set; }
    public bool QCPR { get; set; }
    public bool Keane { get; set; }
    public bool Orsos { get; set; }
    public bool C3M { get; set; }
    public bool Soft { get; set; }
    public bool DIC { get; set; }
    public bool Metalink { get; set; }
    public bool Open_Med { get; set; }
    public bool Muse { get; set; }
    public bool MedQuest { get; set; }
    public string Degree1 { get; set; }
    public string Degree2 { get; set; }
    public string Degree3 { get; set; }
}
}

正在调用save方法,程序在该行崩溃:

existing.AdmPriv = doc.AdmPriv;

有人可以帮助我理解为什么它不会设置实例吗?谢谢!

修改

好的,所以我决定写出我的保存函数中的值,看起来我的表单没有为每一行返回正确的值。它返回了一堆0和falses。我想知道我的表单在索引中是否有问题吗?

2 个答案:

答案 0 :(得分:0)

看起来你有一位不在数据库中的新医生。

检查回复呼叫,让数据库中的医生获取空值。

答案 1 :(得分:0)

Replace foreach statement block like below:
     Doctor existing = db.Doctors.Find(doc.RVH_ID_);
    if (existing != null)
    {            
                existing.AdmPriv = doc.AdmPriv;
                existing.QCPR = doc.QCPR;
                existing.Keane = doc.Keane;
                existing.Orsos = doc.Orsos;
                existing.Soft = doc.Soft;
                existing.C3M = doc.C3M;
    }
    }