Here is my controller action method Calculate
public ActionResult Calculate(List<BOM> bl )
{
ViewBag.Category = new SelectList(db.Category, "ID", "Type");
if (ModelState.IsValid)
{
foreach (var i in bl)
{
db.BOM.Add(i);
}
db.SaveChanges();
}
return View();
}
and View for the Action Method
@model List<My_Inventory.Models.BOM>
@{
ViewBag.Title = "Calculate";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm())
{
<fieldset>
@Html.AntiForgeryToken()
<table class="table">
<tr>
<th>@Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "category" })</th>
<th>@Html.LabelFor(model => model.Make, htmlAttributes: new { @class = "brand" })</th>
<th>@Html.LabelFor(model => model.Item, htmlAttributes: new { @class = "Category" })</th>
<th>@Html.LabelFor(model => model.Quantity, htmlAttributes: new { @class = "PQuantity" })</th>
<th>@Html.LabelFor(model => model.Unit_Cost, htmlAttributes: new { @class = "Unit" })</th>
<th>@Html.LabelFor(model => model.Total_Cost, htmlAttributes: new { @class = "Total" })</th>
</tr>
@for (int i = 0; i < 20; i++)
{
<tr>
<td>
@Html.DropDownListFor(model => model.Category, ViewBag.Category as SelectList, "Select", new { @id = "ddlSelect" + i, @class = "category", onchange = "Getoption(" + i + ");" })
@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
</td>
<td>
@Html.DropDownListFor(model => model.Make, new SelectList(Enumerable.Empty<SelectListItem>(), "Value", "Text"), "Select", new { @id = "ddlsub" + i, @class = "brand", onchange = "GetProducts(" + i + ");" })
@Html.ValidationMessageFor(model => model.Make, "", new { @class = "text-danger" })
</td>
<td>
@Html.DropDownListFor(model => model.Item, new SelectList(Enumerable.Empty<SelectListItem>(), "Value", "Text"), "Select", new { @id = "ddlProduct" + i, @class = "Product" })
@Html.ValidationMessageFor(model => model.Item, "", new { @class = "text-danger" })
</td>
<td>
@Html.TextBoxFor(model => model.Quantity, new { @id = "TextQuantity" + i, @class = "PQuantity", onblur = "Calculatecost(" + i + ");" })
@Html.ValidationMessageFor(model => model.Quantity, "", new { @class = "text-danger" })
</td>
<td>
@Html.LabelFor(model => model.Unit_Cost, "UnitCost", new { @id = "Ucost" + i, @class = "Unit" })
@Html.ValidationMessageFor(model => model.Quantity, "", new { @class = "text-danger" })
</td>
<td>
@Html.LabelFor(model => model.Total_Cost, "TotalCost", new { @id = "TotalCost" + i, @class = "" })
@Html.ValidationMessageFor(model => model.Quantity, "", new { @class = "text-danger" })
</td>
</tr>
}
</table>
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
I am Passing multiple rows of data from view to controller in order to save them to database but it's throwing an error "Object reference not set to an instance of an object." Any Suggestions..whats going wrong??