-2

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??

SimpleBeat
  • 747
  • 1
  • 10
  • 20
M. S
  • 5
  • 6
  • Are you sure you copied the right code? Because your model is a List and you have accessed individual object properties like model.Category!! It should have been something like Model[i].Category. – Amanvir Mundra Apr 28 '17 at 06:08
  • 1
    Show the correct code - what you have shown will not even compile. –  Apr 28 '17 at 06:10
  • @AmanvirSinghMundra I am not accessing values from database..I want to save them to database .. – M. S Apr 28 '17 at 06:31
  • It doesn't matter. What I am saying is that the model is passed as a list to the view and you can't access properties of a list like you mentioned in your view code. – Amanvir Mundra Apr 28 '17 at 06:34
  • @Stephen Muecke.. Sir i am trying to accept multiple rows from user and want to save them to database..Basically each group of control is to generate a row of data..and i have total in all 20 groups..Please guide me how i can retrieve multiple rows from view in order to save them to database. – M. S Apr 28 '17 at 06:34
  • In your GET method, initialize a `List` and populate it with 20 items, and then pass that model to the view, and in the view use a `for` loop of `EditorTemplate` to generate your form controls - refer [this answer](http://stackoverflow.com/questions/30094047/html-table-to-ado-net-datatable/30094943#30094943) –  Apr 28 '17 at 06:36

1 Answers1

0

First of all you are not passing the model to your view in the Controller, it should be return View(bl); rather than a simple return View();

Second, since you are not passing it to your View, it doesn't get set and therefore the Model is null.

Third, you are passing a List as a model and in the view you are accessing it as a single object. Please check your code and post it again.

progrAmmar
  • 2,606
  • 4
  • 29
  • 58