当收集提交回控制器获取对象引用不设置到对象的实例。 错误(when collection is

2019-09-18 15:26发布

我更新多条记录,我点击提交按钮后,我得到的错误,这表明控制器接收NULL并报告在第36行下面的错误下面是我的控制器,视图和模型的代码。

你调用的对象是空的。 描述:

当前Web请求的执行过程中发生未处理的异常。 请检查堆栈跟踪有关该错误它起源于代码的详细信息和。 异常详细信息:System.NullReferenceException:对象不设置到对象的实例。

    Source Error: 

    Line 34:         public ActionResult UpdateAll(ICollection<Test0> test0s)
    Line 35:         {
    Line 36:             foreach (var tst in test0s)
    Line 37:             {
    Line 38:                 if (ModelState.IsValid)

    Source File: c:\users\rsc_vok\documents\visual studio 2010\Projects\MvcTest0\MvcTest0\Controllers\Test0Controller.cs    Line: 36

这里是模型:

namespace MvcTest0.Models
{
    public class Test0
    {
        public int id { get; set; }
        public int SectnID { get; set; }
        public string Section { get; set; }
        public string Comment { get; set; }
    }

    public class Test0DBContext : DbContext
    {
        public DbSet<Test0> Test0s { get; set; }
    }
}

这里是我的控制器代码:

    public ActionResult UpdateAll(int id=0)
    {
        int sectnid = id;
        List<Test0> records = db.Test0s.ToList();
        var Sectnrecord = from r in records
                        where r.SectnID == sectnid
                        select r;

        return View(Sectnrecord.ToList());
    }

    [HttpPost]
    public ActionResult UpdateAll(ICollection<Test0> test0s)
    {
        foreach (var tst in test0s)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tst).State = EntityState.Modified;
            }
        }
        db.SaveChanges();

        return View(test0s);
    }

这里是我的看法

      @model IEnumerable<MvcTest0.Models.Test0>

      @{
      ViewBag.Title = "UpdateAll";
     }

    <script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

   @using (Html.BeginForm())
   {
       @Html.ValidationSummary(true)

       var sectnHeader = new String[10];
       sectnHeader[0] = "A. QUALITY";
       sectnHeader[1] = "B. VR REFERRAL";
       var items = Model.ToArray();
       var sections = 1;
       for (var sectnLoop = 0; sectnLoop < sections; sectnLoop++)
       {
            <table>
                <tr>
                    <td>
                        <b>@(sectnHeader[sectnLoop])</b>
                    </td>
                </tr>  
            </table>
            <table>
                <tr>
                    <th>SectnID</th>
                    <th>Section</th>
                    <th>Comment</th>
                </tr>

            </table>
           for (var i = sectnLoop * 2; i < sectnLoop * 2 + 2; i++)
           {
               var sctnid = "sectnname" + i;
               @Html.HiddenFor(m => items[i].id)
               @Html.HiddenFor(m => items[i].SectnID)

               <table>
                  <tr>
                    <td>
                        @Html.DisplayFor(m => items[i].SectnID)
                    </td>
                    <td>
                        @Html.EditorFor(m => items[i].Section)
                        @Html.ValidationMessageFor(m => items[i].Section)
                    </td>
                    <td>
                        @Html.EditorFor(m => items[i].Comment)
                        @Html.ValidationMessageFor(m => items[i].Comment)
                    </td>
                 </tr>
                </table> 
           } 
       } 
        <p>
            <input type="submit" value="Save" />
        </p>
   }

Answer 1:

解:

  1. 你必须改变视图模型来支持集合类型.Add()方法:

    @model List<MvcTest0.Models.Test0>

  2. 所有更改@Html.___For()像助手:

     @Html.HiddenFor(m => m[i].id) @Html.HiddenFor(m => m[i].SectnID) @Html.DisplayFor(m => m[i].SectnID) @Html.EditorFor(m => m[i].Section) @Html.ValidationMessageFor(m => m[i].Section) @Html.EditorFor(m => m[i].Comment) @Html.ValidationMessageFor(m => m[i].Comment) 

请注意,模型在帖子中重新回来和IEnumerable有没有方法来添加项目本身,不像泛型列表。



文章来源: when collection is submitted back to controller get Object reference not set to an instance of an object. error