简体   繁体   English

从MVC 4缺少值中查看控制器列表?

[英]List to Controller from View in MVC 4 missing values?

After many years of getting a lot of great advice here, I finally have hit a wall teaching myself MVC4 ASP.net. 经过多年在这里得到很多好建议后,我终于在墙上教自己MVC4 ASP.net了。

I used this post this post to pass a List of Type Class from my controller, to my view, and back to the controller.. 我用这个帖子这个帖子从我的控制器通过Class类型的列表,我的看法,并回到控制器..

public ActionResult SelectProducts()
{
    displayProductsList = db.Products.ToList();
    displayProductsList.ForEach(delegate(Product p)
    {
        //get list of recievables for the product
        GetReceivablesByProductId(p.ProductID).ForEach(delegate(Receivable r)
        {
            //Get count of items in inventory for each recievable                  
            p.CurrentInventory += this.CountItemsByReceivableID(r.RecievableID);
        });                
    });
    return View(FilterProductInventoryList(displayProductsList));
}

And here is my view code.. 这是我的视图代码..

@model List<CarePac2.Models.Product>

@{
    ViewBag.Title = "SelectProducts";
}

<h2>SelectProducts</h2>
@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.ValidationSummary(true)
    <table>
        @*row values*@
        @for (int i = 0; i < Model.Count; i++)
        {
            <tr>
                <td>@Html.DisplayFor(m => m[i].Brand)</td>
                <td>@Html.DisplayFor(m => m[i].ProductName)</td>
                <td>@Html.DisplayFor(m => m[i].UnitType)</td>
                <td>@Html.DisplayFor(m => m[i].SalePrice)</td>
                <td>@Html.DisplayFor(m => m[i].CurrentInventory)</td>
                <td>                  
                    @Html.EditorFor(m => m[i].OrderQuantity)
                    @Html.ValidationMessageFor(m => m[i].OrderQuantity)
                </td>
                <td></td>
            </tr>
        }
    </table>
    <p>
        <input type="submit" value="Save" />
        @*<input type="submit" value="Cancel" />*@
    </p>
}

@section Scripts {
@Scripts.Render("~/bundles/jqueryval")

Here the view show it has values for the List that was passed from controller to view.. 这里视图显示它具有从控制器传递到视图的List的值。

Visually the view displays the data correctly (apparently i can't post an image of it until i have 10 reputation to post images) 视觉上视图正确显示数据(显然我不能发布它的图像,直到我有10个声望发布图像)

when i hit submit and return to the controller: 当我点击提交并返回控制器时:

 [HttpPost]
 [ValidateAntiForgeryToken]
 public ActionResult SelectProducts(List<Product> selectedProducts)
 {
     if (ModelState.IsValid)
     {
     }
 }

The variable selectedProducts Is NOT NULL. 变量selectedProducts不是NULL。 The list has 3 Product items in it, however, as you can see in the image below in the debugger, even though i have 3 product items, none of the values exist from when the List of Product was originally passed to the view... 该列表中包含3个产品项,但是,正如您在调试器中的下图中所示,即使我有3个产品项,从产品列表最初传递到视图时也不存在任何值。 。

for example (since i can't post images yet): 例如(因为我还不能发布图像):

selectedProducts[0].ProductID=0
selectedProducts[0].ProductName=null
selectedProducts[1].ProductID=0
selectedProducts[1].ProductName=null

You need to use @Html.HiddenFor() : 你需要使用@Html.HiddenFor()

@Html.HiddenFor(m => m[i].ProductID)
@Html.HiddenFor(m => m[i].ProductName)

This will send the data back to the controller. 这会将数据发送回控制器。 This creates an <input type="hidden"> that will be part of the form POST. 这将创建一个<input type="hidden"> ,它将成为POST表单的一部分。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM