2014-09-05 106 views
0

我想將視圖中的值傳遞給MVC中的控制器。我正在使用ViewModel,通常這些值只要名稱相同就會綁定到屬性。但是,因爲值是通過foreach循環生成的,所以這些值的名稱與視圖模型中的屬性名稱不匹配。MVC:從視圖傳遞價值到控制器

我正在通過將值分配給Razor中的變量來解決這個問題。然而,我的一個值是在窗體上的文本框中,值並沒有傳遞給控制器​​,我無法弄清楚爲什麼。

單擊按鈕時,我得到一個空例外。

VIEW代碼如下:

@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" [email protected] /> 
          <input type="text" hidden="true" name="Price" [email protected] /> 
          <input type="text" hidden="true" name="Quantity" [email protected] /> 
          @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 }); 
     } 

回答

3

不要使用的foreach代碼。改爲使用for-loop,並在此範圍內使用索引限定屬性的完整路徑。

更好的是:使用編輯或DisplayTemplate作爲ShoppingCartProductItem。這也將保持你的道路。

1

你必須改用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<ShoppingCartProductItem>,看到Model Binding To A List

+0

我試過,但沒有奏效 - 感謝思想 – ccocker 2014-09-05 17:22:49

0

你的文本框,以便值的表格中。

嘗試像下面

@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" [email protected] /> 
          <input type="text" hidden="true" name="Price" [email protected] /> 
          <input type="text" hidden="true" name="Quantity" [email protected] /> 
          @Html.Hidden("returnUrl", Request.Url.PathAndQuery) 
          <input type="submit" class="btn btn-success" value="Add to cart" /> 
         } 

        </div> 

      </td> 
     </tr> 
    } 
} 
+0

我試着但它沒有區別 - 謝謝你的想法 – ccocker 2014-09-05 17:23:19

0

我解決了這個在短期內通過使用新的和強制參數的名稱。

@ Html.HiddenFor(modelItem => t.NoOfUsers,新{名稱= 「NoOfUsers」 ID = 「NoOfUsers」})