MVC WebGrid 中的表单
我遇到了麻烦,我需要一个每行都有一个表单的网络网格,我实现了这一点,但是当我单击“提交”按钮时,控制器没有收到输入文本。
这是视图的代码:
@grid.GetHtml(
tableStyle: "mGrid",
headerStyle: "head",
alternatingRowStyle: "alt",
rowStyle: "altRow",
columns: grid.Columns(
grid.Column(columnName: "Id", header: "Id", style: "prefix"),
grid.Column(columnName: "Trademark", header: "Marca", style: "trademark"),
grid.Column(columnName: "Price", header: "Precio", style: "price", format: @<text>@item.Price.ToString("N2")</text>),
grid.Column(format: (item) =>
{
System.Text.StringBuilder html = new System.Text.StringBuilder();
html.Append("<form action=\"/Cart/AddToCart\" method=\"get\">");
html.Append("<input type=\"text\" value=\"\" style=\"width:50px; text-align:center; \" name=\"quantity\" id=\"quantity\" />");
html.Append("<input type=\"submit\" value=\"Agregar\" class=\"btnAdd\" />");
html.Append("<input type=\"hidden\" name=\"productId\" value=\"" + item.Value.Id + "\"/>");
html.Append("<input type=\"hidden\" name=\"returnUrl\" value=\"" + Request.Url + "\"/>");
html.Append("</form>");
return new HtmlString(html.ToString());
}
)
)
)
这是控制器的一部分:
public class CartController : Controller
{
private IDataRepository repository;
...
public RedirectToRouteResult AddToCart(Cart cart, int productId, int quantity, string returnURL)
{
Product product = repository.Products.FirstOrDefault(p => p.Id == productId);
if (product != null)
cart.AddItem(product, quantity);
return RedirectToAction("Index", new { returnURL });
}
一切编译正常。但是当执行数量始终为空时,我已经尝试将数量作为 int 和 string 得到相同的结果。
任何帮助将不胜感激。 谢谢。
i've a trouble, i need a webgrid with a form for each row, i achieved this, but when i click the submit button the controller don't recieve an input text.
This is the code for the view:
@grid.GetHtml(
tableStyle: "mGrid",
headerStyle: "head",
alternatingRowStyle: "alt",
rowStyle: "altRow",
columns: grid.Columns(
grid.Column(columnName: "Id", header: "Id", style: "prefix"),
grid.Column(columnName: "Trademark", header: "Marca", style: "trademark"),
grid.Column(columnName: "Price", header: "Precio", style: "price", format: @<text>@item.Price.ToString("N2")</text>),
grid.Column(format: (item) =>
{
System.Text.StringBuilder html = new System.Text.StringBuilder();
html.Append("<form action=\"/Cart/AddToCart\" method=\"get\">");
html.Append("<input type=\"text\" value=\"\" style=\"width:50px; text-align:center; \" name=\"quantity\" id=\"quantity\" />");
html.Append("<input type=\"submit\" value=\"Agregar\" class=\"btnAdd\" />");
html.Append("<input type=\"hidden\" name=\"productId\" value=\"" + item.Value.Id + "\"/>");
html.Append("<input type=\"hidden\" name=\"returnUrl\" value=\"" + Request.Url + "\"/>");
html.Append("</form>");
return new HtmlString(html.ToString());
}
)
)
)
And this is part of the controller:
public class CartController : Controller
{
private IDataRepository repository;
...
public RedirectToRouteResult AddToCart(Cart cart, int productId, int quantity, string returnURL)
{
Product product = repository.Products.FirstOrDefault(p => p.Id == productId);
if (product != null)
cart.AddItem(product, quantity);
return RedirectToAction("Index", new { returnURL });
}
Everything compile Ok. But when execute quantity always is null, i have already tried quantity as int and string with the same result.
Any help will be wellcome.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,您可以删除 URL 参数和关联的隐藏字段。使用 Request.Referrer 返回上一个操作。
其次,数量不能为空,它可以为零或非零,但整数不能为空。
第三,Cart从哪里来?如果这是域对象,我会将 id 传递给操作并在操作中加载购物车。我还将这些原语组合到特定于上下文的 DTO 中。
其中 AddToCartCommand 是
to start you can drop the URL argument and associated hidden field. use Request.Referrer to return to the previous action instead.
second, quantity cannot be null, it would either be zero or non-zero, but an integer cannot be null.
third, where is Cart coming from? If this is domain object, I would pass the id to the action and load the cart in the action. I would also combine the primatives into a context specific DTO.
where AddToCartCommand is