1

I am trying to add a row to my table when a user inserts data and clicks add button..

My controller is as follows:

[HttpPost]
public ActionResult Index(PurchaseOrder purchaseorder,String btnSubmit)
{
    ViewBag.paymenttypes = Constants.PaymentType();
    ViewBag.supplierid = new SelectList(db.suppliers, "supplierid", "suppliername");
    ViewBag.productid = new SelectList(db.products, "productid", "productname");


    switch (btnSubmit)
    {
        case "Add":
            purchaseorder.purchasedetails.Add(purchaseorder.detail);
            break;
        case "Save":
            break;
    }

    return View(purchaseorder);
}

View is as follows:

@using (Html.BeginForm("Index","PurchaseOrder",FormMethod.Post))
 {

 {
    @Html.ValidationSummary(true)
     @Html.ValidationMessageFor(model => model.purchase.billno)
 <div class="block-content collapse in">
 <div style="float:left; margin-left:20px;">@Html.TextBoxFor(model => model.purchase.purchasedate, new { @readonly = "readonly" })</div>
<div style="float:left; margin-left:20px;">@Html.TextBoxFor(model => model.purchase.billno, new { @Value = "Enter Bill Number" })</div>
<div style="float:left; margin-left:20px;"> @Html.DropDownListFor(model => model.purchase.cashcredit,
new SelectList(ViewBag.paymenttypes, "key", "value"))</div>
<div style="float:left; margin-left:20px;">@Html.DropDownList("supplierid", "Select Supplier")</div>
<div style="clear:both;"></div>
<hr />
<table style="margin-left:10px;" class="table table-striped" id="details">
    <tr>
        <th>
            Product
        </th>
        <th>
            Cost Price
        </th>
        <th>
            Quantity
        </th>
        <th>
            Selling Price
        </th>
    </tr>
    <tr>
        <td>
           @Html.DropDownList("productid", "Select Product")
        </td>
        <td>
            @Html.TextBoxFor(model => model.detail.costprice)
         </td>
        <td>
            @Html.TextBoxFor(model => model.detail.quantity)
         </td>
        <td>
          @Html.TextBoxFor(model => model.detail.sellingprice)
        </td>
        <td>

               </td>
    </tr>
    @foreach (var item in Model.purchasedetails)
    {
    <tr id="abc">
        <td >
            @Html.DisplayFor(modelItem => item.product.productcode)
        </td>
        <td >
            @Html.DisplayFor(modelItem => item.costprice)
        </td>
        <td>
            @Html.DisplayFor(modelItem => item.quantity)
        </td>
        <td >
            @Html.DisplayFor(modelItem => item.sellingprice)
        </td>
    </tr>
    }
</table>
 <input type="submit" id="Add" name="btnSubmit", value="Add" />
  <input type="submit" id="Save" name="btnSubmit", value="Save" />

   </div>

All I am trying to do is when user adds a new data to the table row and presses add button. It must get added to the model as well as table.

I am just getting null pointer exception. Can anyone please help me.

Here is the stack trace of the exception Server Error in '/' Application. 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 44:             {
Line 45:                 case "Add":
Line 46:                     purchaseorder.purchasedetails.Add(purchaseorder.detail);
Line 47:                     break;
Line 48:                 case "Save":
Source File: C:\Users\Ayush Raj Aryal\Desktop\SidhhaBaba\SidhhaBaba\siddha-baba-project\SiddhaBaba\SiddhaBaba\Controllers\Admin\PurchaseOrderController.cs    Line: 46

Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
SiddhaBaba.Controllers.PurchaseOrderController.Index(PurchaseOrder purchaseorder, String btnSubmit) in 
C:\Users\Ayush Raj Aryal\Desktop\SidhhaBaba\SidhhaBaba\siddha-baba-project\SiddhaBaba\SiddhaBaba\Controllers\Admin\PurchaseOrderController.cs:46

Could anyone please help me out with this

3
  • What line of code has the NullReferenceException? Commented Jun 1, 2013 at 8:14
  • null pointer exception Did you mean NullReferenceException? It'd be nice if you posted exception stacktrace. Commented Jun 1, 2013 at 8:16
  • I have added the stack trace too now.. Commented Jun 2, 2013 at 1:55

1 Answer 1

1

I think you're getting the exception at this line:

purchaseorder.purchasedetails.Add(purchaseorder.detail);

The reason for this is that purchaseorder.purchasedetails probably isn't set, because you are not submitting any purchasedetails.

You are listing the purchaseddetails, but using @Html.DisplayFor doesn't generated <input> elements. If you want the purchaseddetails data to be submitted you'd have to do something along the line of:

@foreach (var item in Model.purchasedetails)
{
    <tr id="abc">
        <td >
            @Html.DisplayFor(modelItem => item.product.productcode)
            @Html.HiddenFor(modelItem => item.product.productcode)
        </td>
    </tr>
Sign up to request clarification or add additional context in comments.

3 Comments

I tried doing so but the data is not submitted yet. I am using a wrapper model to wrap the classes used here. The class is as follows: public class PurchaseOrder { public purchase purchase { get; set; } public purchasedetail detail { get; set; } public List<purchasedetail> purchasedetails { get; set; } } Can it be the cause?
This answer should help you more with binding complex models, where the most preferable way is the EditorTemplate
your link worked out for me.. I did it using IList, I would also give a try using editor template as suggested there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.