我正在研究MVC3 ASP.Net应用程序。我正在试图弄清楚如何设置Quantity
变量,所以当我将Html.ActionLink
传递给控制器时,它具有正确的数字。这是视图的代码
@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 :(得分:1)
以下代码在包含文本框时适用于我。我在使用html助手时遇到了麻烦,所以我直接为输入框编写了代码。我希望这会有所帮助。
grid.Column("Quantity", format: @<text><input name="Quantity" type="text" value="@item.Quantity"</text>))
答案 1 :(得分: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");
}