Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update multiple records at once in asp.net mvc

Tags:

I'm trying to make a website using asp.net mvc 4 & EF6 where I want to update multiple rows all at once. But for some reason, it's not working & I get an error like this,

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

Here are my codes,

Controller

[HttpPost]
    public ActionResult MakeDue(List<BillCheck> BillLists)
    {
        if (Session["username"] != null)
        {
            if (ModelState.IsValid)
            {
                foreach (var BillId in BillLists)
                {
                    var getDue = db.BillChecks.Where(p => p.id == BillId.id).FirstOrDefault();
                    getDue.due = BillId.due;
                }
                db.SaveChanges();
                return RedirectToAction("Success");
            }
            else
            {
                return RedirectToAction("Failed");
            }
        }
        else
        {
            return RedirectToAction("Login");
        }
    }

View

@using (Html.BeginForm("MakeDue", "Home"))
{
    @Html.ValidationSummary(true)
    @foreach(var item in Model.DueList)
    {
        @Html.HiddenFor(modelItem => item.id)
        <tr>
            <td>@Html.DisplayFor(modelItem => item.flat)</td>
            <td>@Html.DisplayFor(modelItem => item.name)</td>
            <td>@Html.TextBoxFor(modelItem => item.due)</td>
        </tr>
    }
    <input type="submit" class="btn btn-success" value="Update" />
}

Is there something wrong in my code? How can I update all the inputs for due given at once?