Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8151699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T15:28:15+00:00 2026-06-06T15:28:15+00:00

I am updating multiple records and after I click submit button I get the

  • 0

I am updating multiple records and after I click submit button I get the error, which indicates that controller receives NULL and reports the following error at line 36
Below is the code for my controller, view and the model.

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: 

    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

Here is the model:

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; }
    }
}

And here is my controller code:

    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);
    }

And here is my view

      @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>
   }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T15:28:16+00:00Added an answer on June 6, 2026 at 3:28 pm

    Solution:

    1. You have to change the view model to a collection type which support .Add() method:

      @model List<MvcTest0.Models.Test0>

    2. Change all the @Html.___For() helpers like:

      @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)
      

    Notice that model has to be recreated in post back and IEnumerable has no method to add items to itself, unlike generic List.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found something that works with updating one field at here: http://www.karlrixon.co.uk/articles/sql/update-multiple-rows-with-different-values-and-a-single-sql-query/ UPDATE person
Want to pause the multiple views from updating when Pause button is pressed In
After updating to php 5.3 one of our systems has developed an interesting bug.
When updating a DataTable with 1850-ish new rows to a FbDataAdapter I get a
After updating to Parsec 3.1 from 2.x, code using many1, such as word =
After updating to XCode 4.3, all my archives built using XCode 4.2 are gone.
Subsonic 3.0 is updating multiple rows instead of just the one it's supposed to
Is there a way to have multiple delayed_job workers updating to the same table?
I have multiple context files. Requirement is: one particular Bean (which makes some configuration
What is the proper query for updating multiple rows in MySQL at the same

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.