我正在開發一個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");
}
你有沒有嘗試設置這樣的值:grid.Column( 「數量」,格式:(項目)=> Html.TextBox( 「數量」,( int)item.Quantity))...應該做的伎倆(未測試) – danielQ
不,它不起作用 – user1496729
請問,你能提供的行動代碼?謝謝。 – danielQ