2013-06-01 121 views
1

我试图将一行添加到我的桌子当用户插入数据和点击添加按钮..数据未从视图传递给控制器​​MVC3

我的控制器如下:

[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); 
} 

查看如下:

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

所有我想要做的是,当用户添加一个新的数据到表行并按下添加按钮。它必须添加到模型以及表格中。

我刚收到空指针异常。谁能帮帮我吗。

以下是异常的堆栈跟踪 '/'应用程序中的服务器错误。 未将对象引用设置为对象的实例。 描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关该错误的更多信息以及源代码的位置。

异常详细信息:

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 

任何人都可以请帮我这个

+0

哪行代码具有'NullReferenceException'? –

+0

'空指针异常'你的意思是'NullReferenceException'吗?如果你发布了异常栈跟踪会很好。 – Leri

+0

我也已经添加了堆栈跟踪.. – Ayush

回答

1

我觉得你要在这条线的异常:

purchaseorder.purchasedetails.Add(purchaseorder.detail); 

这样做的原因是那purchaseorder.purchasedetails可能没有设置,因为你没有提交任何purchasedetails。

您正在列出所购商品,但使用@Html.DisplayFor不会生成<input>元素。如果您想要提交已购买的商品详情数据,则必须按照以下方式执行操作:

@foreach (var item in Model.purchasedetails) 
{ 
    <tr id="abc"> 
     <td > 
      @Html.DisplayFor(modelItem => item.product.productcode) 
      @Html.HiddenFor(modelItem => item.product.productcode) 
     </td> 
    </tr> 
+0

我试过但数据还没有提交。我正在使用包装模型来包装这里使用的类。班级如下:公共班购买订单 { 公开购买购买{get;组; } public purchasedetail detail {get;组; } public List purchasedetails {get;组; } }它可能是原因吗? – Ayush

+0

[这个答案](http://stackoverflow.com/a/11682510/695586)应该可以帮助你更多的绑定复杂的模型,其中最可取的方式是EditorTemplate –

+0

你的链接为我制定出来..我使用它IList,我也会尝试使用编辑器模板。 – Ayush

相关问题