1

I want to be able to batch update data in my db. I Can display data as a list but Retrieving data to update the db results in this error:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

My code is as follows: My Controller

public ActionResult UpdateRecords(int? id)
{
    if (id == null)
    {
        return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
    }

    var view = from m in db.Tbl_Marks
                  join c in db.Tbl_Child on m.ChildID equals c.ChildID
                  where m.AID == id 
                  select new Assessments { Child=c, Marks=m};

    return View(view.ToList());
}

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UpdateRecords(IEnumerable<Assessments> tbm )
{
    if (ModelState.IsValid)
    {       
        foreach(Assessments i in tbm)
        {
            Tbl_Marks m = new Tbl_Marks();
            m.AID = i.Marks.AID;
            m.MID = i.Marks.MID;
            m.MarkAchieved = i.Marks.MarkAchieved;
            m.ChildID = i.Marks.ChildID;
            db.Entry(m).State = System.Data.Entity.EntityState.Modified;
        }
        db.SaveChanges();        

        return RedirectToAction("Assessments");
    }
    return View("Assessments");
}

My View

@model IEnumerable<CEDAR.Assessments>


<h2>Marks</h2>
<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.Child.ChildID)
        </th>

        <th>
            @Html.DisplayNameFor(model => model.Child.Name)
        </th>

        <th>
            @Html.DisplayNameFor(model => model.Child.Surname)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.Marks.MarkAchieved)
        </th>

        <th></th>
    </tr>
    @using (Html.BeginForm("UpdateRecords", "Admin"))
    {
        @Html.AntiForgeryToken()

        foreach (var item in Model)
        {
            @Html.HiddenFor(modelItem => item.Marks.MID)
            @Html.HiddenFor(modelItem => item.Marks.AID)
            @Html.HiddenFor(modelItem => item.Marks.ChildID)
            @Html.HiddenFor(modelItem => item.Child.Surname)
            @Html.HiddenFor(modelItem => item.Child.Grade)
            @Html.HiddenFor(modelItem => item.Child.Disabilities)
            @Html.HiddenFor(modelItem => item.Child.ParentID)
            @Html.HiddenFor(modelItem => item.Child.Enrolled)
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Child.ChildID)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.Child.Name)
                </td>

                <td>
                    @Html.DisplayFor(modelItem => item.Child.Surname)
                </td>
                <td>
                    @Html.EditorFor(modelItem => item.Marks.MarkAchieved, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(modelItem => item.Marks.MarkAchieved, "", new { @class = "text-danger" })

                </td>
            </tr>
        }

        <div class="form-group">
            <div class="col-md-offset-2 col-md-10">
                <input type="submit" value="Save" class="btn btn-primary" />
                <input type="button" value="Cancle" onclick="goBack()" class="btn btn-danger" />
            </div>
        </div>

    }
</table>

My Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CEDAR
{
    public class Assessments
    {        
        public Tbl_Marks Marks { get; set; }
        public Tbl_Child Child { get; set; }
    }
}

Im taking data from two tables and merging it to display as one view. I should be able to update the MarksAchieved fields from the list and then click save. It would then go through the db and update all the MarksAchieved Fields from every item on the list.

haldo
  • 14,512
  • 5
  • 46
  • 52

0 Answers0