【问题标题】:Data not passed from view to controller in mvc3数据未在 mvc3 中从视图传递到控制器
【发布时间】:2013-06-01 06:35:01
【问题描述】:

当用户插入数据并单击添加按钮时,我正在尝试向我的表中添加一行..

我的控制器如下:

[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

谁能帮我解决这个问题

【问题讨论】:

  • 哪行代码有NullReferenceException
  • null pointer exception 你是说NullReferenceException 吗?如果您发布异常堆栈跟踪,那就太好了。
  • 我现在也添加了堆栈跟踪..

标签: asp.net-mvc-3


【解决方案1】:

我认为您在这一行遇到了异常:

purchaseorder.purchasedetails.Add(purchaseorder.detail);

原因是purchaseorder.purchasedetails 可能没有设置,因为您没有提交任何购买的详细信息。

您正在列出购买的详细信息,但使用 @Html.DisplayFor 不会生成 &lt;input&gt; 元素。如果您希望提交购买的详细信息数据,您必须执行以下操作:

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

【讨论】:

  • 我试过了,但是数据还没有提交。我正在使用包装器模型来包装这里使用的类。类如下: public class PurchaseOrder { public purchase purchase { get;放; } 公开购买的详细信息 { 获取;放; } 公共列表 购买的详细信息 { 获取;放; } } 这可能是原因吗?
  • This answer 应该可以帮助您更多地绑定复杂模型,其中最可取的方式是 EditorTemplate
  • 你的链接对我有用。我是使用 IList 完成的,我也会尝试使用那里建议的编辑器模板。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-23
  • 1970-01-01
  • 2017-03-12
  • 1970-01-01
  • 2012-04-03
  • 1970-01-01
相关资源
最近更新 更多