none
MVC Object reference not set to an instance of an object

    Question

  • 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>
    The error:

    Object reference not set to an instance of an object.

    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)

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

    Line 49:         {

    Line 50:             foreach (var survey in mysurveys)

    Line 51:             {

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


    Source File: C:\Users\rsc_vok\Documents\Visual Studio 2010\Projects\MvcMysurvey\MvcMysurvey\Controllers\MysurveyController.cs    Line: 50



    michael vokhgelt

    Monday, 25 June 2012 4:54 PM

All replies

  • 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.

    I hope this helps.


    Please mark this post as answer if it solved your problem. Happy Programming!

    Monday, 25 June 2012 5:01 PM
  • Michael, 

    This forum is for the .Net Base class (system object) and its related libraries

    The Microsoft ASP (and therefore MVC forums are at)

    http://forums.asp.net


    Success
    Cor

    • Proposed as answer by Mike Feng Wednesday, 27 June 2012 9:29 AM
    Monday, 25 June 2012 5:05 PM
  • Thank you for your answer - I realize that it is NULL but I do not understand why it is null - I am passing everything back from the view.

    michael vokhgelt

    Monday, 25 June 2012 5:11 PM
  • Can I transfer my question to that forum or do I need to retype it there.

    Thanks


    michael vokhgelt

    Monday, 25 June 2012 5:12 PM
  • As suggested by Cor you should post this to ASP.NET forums.

    You should try concrete class returned from the View; like List instead of IEnumerable or ICollection. The reason to null is because MVC engine can not create type for you because the UpdateTest requires ICollection, try List instead. Also make sure MySurvey is serializable and it must be because single record code works.


    • Edited by MasaSam Monday, 25 June 2012 5:26 PM
    Monday, 25 June 2012 5:24 PM
  • thanks - I just tried List like this

    [HttpPost]
            public ActionResult UpdateTest(List<Mysurvey> mysurveys)
            {
                foreach (var survey in mysurveys)
                {
                    db.Entry(survey).State = EntityState.Modified;
                }
                db.SaveChanges();
                return View(mysurveys);
            }
    and got exactly the same error.  I am new here and it looks like somehow I set the Model to NULL in a view.  I do not know how to pass it back to the controller after I update the values

    michael vokhgelt

    Monday, 25 June 2012 6:08 PM
  • I've just tried FormCollection instead and it passed the previous error point but now I am getting

    The entity type String is not part of the model for the current context error

    at    db.Entry(survey).State = EntityState.Modified;

            public ActionResult UpdateTest(FormCollection mysurveys)

            {

                System.Diagnostics.Debug.WriteLine("iam in test post" + mysurveys.Count);

                foreach (var survey in mysurveys)

                {

                    db.Entry(survey).State = EntityState.Modified;

                          }

                db.SaveChanges();

                return View(mysurveys);

            }


    michael vokhgelt

    Monday, 25 June 2012 7:09 PM