2013-06-01 50 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

相關問題