MVC对象引用未设置为对象的实例 [英] MVC Object reference not set to an instance of an object

查看:67
本文介绍了MVC对象引用未设置为对象的实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了所有有关此错误的帖子,但没有任何帮助。我正在使用MVC与ASPX(C#)中的视图。在简单视图中编辑数据后,我正在更新数据库中的记录。当我一次处理一个记录(Controller EDIT方法和EDIT
视图)时,一切正常。当我处理多个记录(Controller UpdateTest和UPDATETEST视图)时,在视图中单击"提交"按钮后,我在Controller中的HTTPPOST方法中出现错误。

I looked at all of the postings with this error but none helps. I am using MVC with the views in ASPX (C#). I am updating records in the database after editing the data in simple views. When I process one record at a time (Controller EDIT methods and EDIT view) all works fine. When I process multiple records (Controller UpdateTest and UPDATETEST view) I get an error in my HTTPPOST method in Controller after clicking the Submit button in a view.

这里是示例代码的错误控制器和视图。 单个记录的类似代码工作正常。

here is an error with the sample codes of the CONTROLLER and the VIEW.  Similar code for a single record works fine.

//CONTROLLER
//GET
        public ActionResult UpdateTest(int id = 0)
        {
            List<Mysurvey> mysurveys = db.Mysurveys.ToList();
            return View(mysurveys.ToList());
        }
//POST
        [HttpPost]
        public ActionResult UpdateTest(ICollection<Mysurvey> mysurveys)
        {
            foreach (var survey in mysurveys)
            {
                db.Entry(survey).State = EntityState.Modified;
            }
            db.SaveChanges();
            return View(mysurveys);
        }
//VIEW
<%@ Page Language="C#"  MasterPageFile="~/Views/Shared/Site.master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcMysurvey.Models.Mysurvey>>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
    Edit
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Edit</h2>
<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) %>
    <fieldset>
        <legend>Mysurvey</legend>
        <% foreach (var item in Model) {  %>
        <%: Html.HiddenFor(model => item.ID) %>
        <%: Html.HiddenFor(model => item.SurveyID) %>
        <div class="editor-label">
            <%: Html.LabelFor(model => item.Comment) %>
        </div>
        <div class="editor-field">
            <%: Html.EditorFor(model => item.Comment) %>
            <%: Html.ValidationMessageFor(model => item.Comment) %>
        </div>
        <%} %>
        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
<% } %>
<div>
    <%: Html.ActionLink("Back to List", "Index") %>
</div>
</asp:Content>


错误:

对象引用未设置为对象的实例。

描述:
在执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪,以获取有关错误的更多信息以及代码中源自
的位置。

异常详情:
System.NullReferenceException:未设置对象引用到一个对象的实例。

源错误:
  (控制器 - 见以下HttpPost  
UpdateTest( ICollection < Mysurvey > mysurveys)

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:  (CONTROLLER – SEE BELOW HttpPost  UpdateTest(ICollection<Mysurvey> mysurveys)

第48行:        
public ActionResult UpdateTest(ICollection< Mysurvey> mysurveys)

Line 48:         public ActionResult UpdateTest(ICollection<Mysurvey> mysurveys)

第49行:        
{

Line 49:         {

第50行:            
foreach(var survey in mysurveys)

Line 50:             foreach (var survey in mysurveys)

第51行:            
{

Line 51:             {

第52行:            
      db.Entry(survey).State = EntityState.Modified;

Line 52:                 db.Entry(survey).State = EntityState.Modified;



源文件:
C:\ Users \rsc_vok \Documents\Visual Studio 2010 \Projects\MvcMysurvey \ MVcMysurvey \Controllers\MysurveyController.cs
  行:
50

michael vokhgelt

michael vokhgelt

推荐答案

看起来你的mysurveys集合是null。你可以在下面添加一个空检查,

looks like your mysurveys colletion is null. You may add a null check there as below,

[HttpPost]
public ActionResult UpdateTest(ICollection<Mysurvey> mysurveys)
{
    if(mysurveys != null)
    {
       foreach (var survey in mysurveys)
       {
           db.Entry(survey).State = EntityState.Modified;
       }
    }

    db.SaveChanges();
    return View(mysurveys);
}

但是,这不应该是一个明智的解决方案。你必须分析为什么这个集合是有效的。

But, this should not be idea solution. You must analyze why the collection is empy.

我希望这会有所帮助。


这篇关于MVC对象引用未设置为对象的实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆