【问题标题】:MVC: Passing Value from View to ControllerMVC:将值从视图传递到控制器
【发布时间】:2014-09-05 15:44:36
【问题描述】:

我正在尝试将值从视图传递到 MVC 中的控制器。我使用的是 ViewModel,通常只要名称相同,这些值就会绑定到属性。但是,由于值是通过 foreach 循环生成的,因此值的名称与视图模型中的属性名称不匹配。

我正在通过将值分配给 Razor 中的变量来解决此问题。但是,我的值之一是在表单上的文本框中,并且该值没有传递给控制器​​,我无法弄清楚原因。

单击按钮时出现空异常。

查看代码如下:

@model PagedList.IPagedList<Mojito.Domain.ViewModels.ShoppingCartProductItem>
@using System.Web.UI.WebControls
@using PagedList.Mvc;
<link href="~/Content/PagedList.css" rel="stylesheet" type="text/css" />
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Mojito Products</h2>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().Description)
        </th>

        <th>
            @Html.ActionLink("Price", "Index", new { sortOrder = ViewBag.SortByPrice, currentFilter = ViewBag.CurrentFilter })
        </th>
        <th>
            @Html.DisplayNameFor(model => model.FirstOrDefault().Quantity)
        </th>
        <th>

        </th>

        <th></th>
    </tr>

    @foreach (var item in Model)
    {
        <tr>

            <td>
                @Html.DisplayFor(modelItem => item.Description)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Price)
            </td>
            <td>
                @Html.TextBoxFor(modelItem => item.Quantity)
            </td>


            <td>
                @{string Description = item.Description;}
                @{decimal Price = item.Price;}
                @{int Quantity = item.Quantity; }

                @using (Html.BeginForm("AddToCart", "ShoppingCart", FormMethod.Post))
                {
                    <div class="pull-right">
                        @if (Request.Url != null)
                        {
                            <input type="text" hidden="true" name="Description" value=@Description />
                            <input type="text" hidden="true" name="Price" value=@Price />
                            <input type="text" hidden="true" name="Quantity" value=@Quantity />
                            @Html.Hidden("returnUrl", Request.Url.PathAndQuery)
                            <input type="submit" class="btn btn-success" value="Add to cart" />
                        }

                    </div>
                }
            </td>
        </tr>
    }


</table>
<div class="col-md-12">
    Page @(Model.PageCount < Model.PageNumber ? 0 : Model.PageNumber) of @Model.PageCount
</div>
@Html.PagedListPager(Model, page => Url.Action("Index",
        new { page, sortOrder = ViewBag.CurrentSort, currentFilter = ViewBag.CurrentFilter }))

控制器代码如下

public ActionResult AddToCart(Cart cart, MojitoProduct product, string returnUrl, int Quantity =1)
        {

            if (product != null)
            {
                cart.AddItem(product, Quantity);
            }
            return RedirectToAction("Index", new { returnUrl });
        }

【问题讨论】:

    标签: c# asp.net-mvc razor viewmodel


    【解决方案1】:

    不要使用 foreach。改用 for 循环,并在其中使用索引限定属性的完整路径。

    更好的是:为ShoppingCartProductItem 使用 Edit- 或 DisplayTemplate。这也将保持您的路径。

    【讨论】:

      【解决方案2】:

      你必须使用 for 循环而不是 foreach:

      @for (int i=0;i < Model.Count; i++)
      {
              <tr>
      
                  <td>
                      @Html.DisplayFor(modelItem => Model[i].Description)
                  </td>
                  <td>
                      @Html.DisplayFor(modelItem => Model[i].Price)
                  </td>
                  <td>
                      @Html.TextBoxFor(modelItem => Model[i].Quantity)
                  </td>
          ..........................
          ..........................
          ..........................
      }
      

      您也可以通过发布List&lt;ShoppingCartProductItem&gt; 使用一个表格发布所有内容,请参阅Model Binding To A List

      【讨论】:

        【解决方案3】:

        您的文本框如此重视表单。

        像下面这样试试

        @using (Html.BeginForm("AddToCart", "ShoppingCart", FormMethod.Post))
        {
            @foreach (var item in Model)
            {
                <tr>
        
                    <td>
                        @Html.DisplayFor(modelItem => item.Description)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Price)
                    </td>
                    <td>
                        @Html.TextBoxFor(modelItem => item.Quantity)
                    </td>
        
        
                    <td>
                        @{string Description = item.Description;}
                        @{decimal Price = item.Price;}
                        @{int Quantity = item.Quantity; }
        
        
                            <div class="pull-right">
                                @if (Request.Url != null)
                                {
                                    <input type="text" hidden="true" name="Description" value=@Description />
                                    <input type="text" hidden="true" name="Price" value=@Price />
                                    <input type="text" hidden="true" name="Quantity" value=@Quantity />
                                    @Html.Hidden("returnUrl", Request.Url.PathAndQuery)
                                    <input type="submit" class="btn btn-success" value="Add to cart" />
                                }
        
                            </div>
        
                    </td>
                </tr>
            }
        }
        

        【讨论】:

        • 我试过了,但没有任何区别 - 谢谢你的想法
        【解决方案4】:

        我通过使用 new 并强制参数名称在短期内解决了这个问题。

        @Html.HiddenFor(modelItem => t.NoOfUsers, new { Name = "NoOfUsers", id = "NoOfUsers" })

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2016-12-28
          • 2012-01-19
          • 1970-01-01
          • 1970-01-01
          • 2016-06-08
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多