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
NullReferenceException?null pointer exceptionDid you meanNullReferenceException? It'd be nice if you posted exception stacktrace.