对象引用未设置为mvc4中对象的实例

时间:2014-03-18 05:31:14

标签: asp.net-mvc-4

* 我有类似的模型类: *

public class Timesheetmodel
 {
    ResLandEntities res = new ResLandEntities();
   public int WEEK_CAL_ID { get; set; }
    public int COMP_ID { get; set; }
    public int RES_ID { get; set; }
    public int PROJ_ID { get; set; }
    public string DESCR { get; set; }
    public int SUN_HRS { get; set; }
    public int MON_HRS { get; set; }
    public int TUE_HRS { get; set; }
    public int WED_HRS { get; set; }
    public int THU_HRS { get; set; }
    public int FRI_HRS { get; set; }
    public int SAT_HRS { get; set; }

    public string IS_DELETED { get; set; }
    public string CR_BY { get; set; }
    public DateTime DT_CR { get; set; }
    public string MOD_BY { get; set; }
    public DateTime DT_MOD { get; set; }

    public List<Timesheetmodel> GetTimeSheetDetails { get; set; }


}

在视图中:

        <table id="dataTable" style="width: 80%; border: none">
             <tbody>

           @for (int i = 1; i <=Convert.ToInt32(Session["count"]); i++)
                                {
                <tr>
                                        <td style="width: 100px">

                                            @* @Html.DropDownListFor(m => m.GetTimeSheetDetails[i].PROJ_ID, (SelectList)Model.getprojects(), "--Choose Your Project--")*@
                                        </td>
                                        <td>

                                            @Html.TextBox("txtTask")
                                        </td>
                                        <td>
                                     @Html.TextBoxFor(m => m.GetTimeSheetDetails[i].SUN_HRS, new { style = "width:50px; height:30px;" })

                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => m.GetTimeSheetDetails[i].MON_HRS, new { style = "width:90px; height:30px;" })

                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => m.GetTimeSheetDetails[i].TUE_HRS, new { style = "width:90px; height:30px;" })

                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => m.GetTimeSheetDetails[i].WED_HRS, new { style = "width:90px; height:30px;" })
                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => m.GetTimeSheetDetails[i].THU_HRS, new { style = "width:90px; height:30px;" })
                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => m.GetTimeSheetDetails[i].FRI_HRS, new { style = "width:90px; height:30px;" })
                                        </td>
                                        <td>
                                            @Html.TextBoxFor(m => m.GetTimeSheetDetails[i].SAT_HRS, new { style = "width:90px; height:30px;" })
                                        </td>
                                        <td>
                                            <input type="image" src="~/Img/delete.PNG" value="Delete Row" onclick="deleteRow(this)" style="margin-top: 5px;" />
                                        </td>

                                    </tr>

                                          }

                                <tr>
                                    <td>
                                        <input type="submit" value="Update" class="btnstyle" name="btn" />
                                    </td>
                                </tr>
                            </tbody>
                        </table>

和在控制器中:

   [HttpPost]
    public ActionResult Timesheet(string btn, Timesheetmodel objts)
    {

        TIMESHEET ts = new TIMESHEET();       
            if (btn == "Update")
            {
                foreach (var item in objts.GetTimeSheetDetails)//Getting Error Here as Object is not set to reference of the object.
                {
                    item.PROJ_ID = ts.PROJ_ID;
                    item.DESCR = ts.DESCR;
                    ts.SUN_HRS = item.SUN_HRS;
                    ts.MON_HRS = item.MON_HRS;
                    ts.IS_DELETED = "N";
                    ts.TUE_HRS = item.SAT_HRS;
                    ts.SAT_HRS = item.SAT_HRS;
                    ts.SAT_HRS = item.SAT_HRS;
                    ts.SAT_HRS = item.SAT_HRS;
                    ts.SAT_HRS = item.SAT_HRS;
                    objTimeSheet.TIMESHEETs.Add(ts);
                    objTimeSheet.SaveChanges();
                }
            }
            if (btn == "AddRecord")
            {
                int ses = Convert.ToInt32(Session["count"]);
                ses++;
                Session["count"] = ses;
            }
        return View();
    }

我已将Everything设置为完美,但显示错误为

  

&#34;对象引用未设置为对象的引用&#34;。

我有提交错误的地方。请任何人帮助我。

2 个答案:

答案 0 :(得分:1)

使用

将模型正确绑定到帖子上的操作参数
TextBoxFor( m => m.GetTimeSheetDetails[ i ].XXX_HRS

在视图中调用 - 所以那里没有问题。

如果“对象引用未设置为对象实例”错误发生在我的位置,则单击更新按钮时视图中没有任何TimeSheetDetail输入(当Session [“Count”] == 0时)。原因是DefaultModelBinder搜索POSTed表单值以查找绑定到模型的数据,但由于视图中不存在输入,因此无法找到任何数据。因此

objts.GetTimeSheetDetails

没有任何绑定到它的值并保持为空。

如果没有类似这样的项目

,我建议您考虑删除视图中的更新按钮
@if( 0 < Convert.ToInt32(Session[ "count" ] )
{
    <tr>
        <td><input type="submit" value="Update" class="btnstyle" name="btn" /></td>
    </tr>
}

答案 1 :(得分:0)

问题是您无法将列表传递给服务器。基本上你正在做的只是为列表中的每个值渲染一个文本框,但没有id,更不用说唯一的id了。换句话说,一旦渲染这些文本框,就会丢失绑定。表格帖子需要作为关键值对的表单数据发送回服务器;这种设计打破了这一点,因此您的控制器无法将值绑定/反序列化回viewmodel属性,因此它最终为null。

如果您将@ Html.EditorFor(m =&gt; m.GetTimeSheetDetails)纳入您的设计,MVC可以为您提供帮助。这将为每个控件分配一个预期格式的唯一ID,该格式可以传递给操作并反序列化为适当的列表。这会使您的表格布局复杂化,但可以使用css解决。