2012-07-03 66 views
1

我正在開發一個MVC3 ASP.Net應用程序。我試圖弄清楚如何設置Quantity變量,因此當我通過Html.ActionLink將其傳遞給控制器​​時,它具有正確的編號。下面是該視圖的代碼使用@ Html.TextBox設置變量MVC3 ASP.Net

@model IEnumerable<GreatVideosTrainingApplication.Models.Candy> 
@{ 
    ViewBag.Title = "Great Videos"; 
    List<GreatVideosTrainingApplication.Models.Candy> candies = new List<GreatVideosTrainingApplication.Models.Candy>(); 
    foreach (var candy in Model) 
    { 
     candies.Add(candy); 
    } 
    var grid = new WebGrid(candies); 
    var Quantity = 0; 
} 
<p>Welcome To Great Videos! The best source for your favorite DVDs and Blu-Rays</p> 
<img src ="/Content/Images/dvd50.jpg" /> 
<p></p> 
<img src="/Content/Images/bluray.jpg" /> 
<form method="post" action="/ShoppingCart/AddToCandyCart/"+item.CandyID > 

@grid.GetHtml(@columns: grid.Columns(
           grid.Column("Name"), 
           grid.Column("Price"), 
           grid.Column("Quantity", format: (item) => @Html.TextBox("Quantity", @Quantity)), 
           grid.Column("AddToCart", format: (item) => Html.ActionLink("Add To Cart", "AddToCandyCart", "ShoppingCart", new { id = item.CandyID, quantity = @Quantity }, "")) 
        ) 
) 

</form> 

我試圖設置與Html.TextBox數量值,但它不工作。請記住,我不知道JavaScript,而且我對MVC3非常陌生。任何和所有的幫助,不勝感激。包括一個文本框時

public ActionResult AddToCandyCart(int id, FormCollection values) 
    { 
     // Add it to the shopping cart 
     var quantity = values["Quantity"]; 
     var cart = ShoppingCart.GetCart(this.HttpContext); 
     // Retrieve the video from the database 
     var addedCandy = storeDB.Candies.Single(Candy => Candy.CandyID == id); 
     cart.AddToCandyCart(addedCandy, int.Parse(quantity)); 
     // Go back to the main store page for more shopping 
     return RedirectToAction("Index"); 
    } 
+0

你有沒有嘗試設置這樣的值:grid.Column( 「數量」,格式:(項目)=> Html.TextBox( 「數量」,( int)item.Quantity))...應該做的伎倆(未測試) – danielQ

+0

不,它不起作用 – user1496729

+0

請問,你能提供的行動代碼?謝謝。 – danielQ

回答

1

下面的代碼爲我工作。我在使用html助手時遇到了麻煩,所以我直接編寫了輸入框的代碼。我希望這有幫助。

grid.Column("Quantity", format: @<text><input name="Quantity" type="text" value="@item.Quantity"</text>)) 
+0

這樣做會不斷給我一個錯誤:錯誤參數3:無法從'lambda表達式'轉換爲'System.Func <動態,對象>' – user1496729

1

找出混合各種來源的問題。想要感謝大家。這是觀點。

@model IEnumerable<GreatVideosTrainingApplication.Models.Candy> 
@{ 
    ViewBag.Title = "Great Videos"; 
    List<GreatVideosTrainingApplication.Models.Candy> candies = new List<GreatVideosTrainingApplication.Models.Candy>(); 
    foreach (var candy in Model) 
    { 
     candies.Add(candy); 
    } 
    var grid = new WebGrid(candies); 
    var Quantity = 0; 
} 

<p>Welcome To Great Videos! The best source for your favorite DVDs and Blu-Rays</p> 
<img src ="/Content/Images/dvd50.jpg" /> 
<p></p> 
<img src="/Content/Images/bluray.jpg" /> 
<form method="post" action="../ShoppingCart/AddToCandyCart/" > 
    @using (Html.BeginForm()) { 
     @grid.GetHtml(@columns: grid.Columns(
             grid.Column("Name"), 
             grid.Column("Price"), 
             grid.Column("Quantity", format: @<text><input name="Quantity" type="text" value="@Quantity"</text>), 
             grid.Column("AddToCart", format: @<text><input type="submit" value="Add To Cart" name="submit" /></text>) 
          ) 
    ) 
} 
</form> 

這裏的動作控制器

[HttpPost] 
public ActionResult AddToCandyCart(FormCollection values) 
{ 
     int id = 1; 
     string[] quantities = values["Quantity"].Split(','); 
     foreach (var item in quantities) 
     { 
      try 
      { 
       int quantity = int.Parse(item); 
       if (quantity >= 1) 
       { 
        // Add the candy to the shopping cart 
        var addedCandy = storeDB.Candies.Single(Candy => Candy.CandyID == id); 
        var cart = ShoppingCart.GetCart(this.HttpContext); 
        cart.AddToCandyCart(addedCandy, int.Parse(item)); 
       } 
      } 
      catch (Exception e) 
      { 
       return View("Failed"); 
      } 

      id++; 
     } 
     // Go back to the main store page for more shopping 
     return RedirectToAction("Index"); 
}