MVC WebGrid 中的表单

发布于 2024-12-24 20:43:23 字数 2209 浏览 4 评论 0原文

我遇到了麻烦,我需要一个每行都有一个表单的网络网格,我实现了这一点,但是当我单击“提交”按钮时,控制器没有收到输入文本。

这是视图的代码:

@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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

层林尽染 2024-12-31 20:43:23

首先,您可以删除 URL 参数和关联的隐藏字段。使用 Request.Referrer 返回上一个操作。

其次,数量不能为空,它可以为零或非零,但整数不能为空。

第三,Cart从哪里来?如果这是域对象,我会将 id 传递给操作并在操作中加载购物车。我还将这些原语组合到特定于上下文的 DTO 中。

AddToCart(AddToCartCommand input)
{
     var cart = repository.Carts.First(input.CartId);
     var product = repository.Products.First(input.ProductId);
     cart.Add(product, input.Quantity);

     return RedirectToAction(Request.Referrer);
}

其中 AddToCartCommand 是

class AddToCartCommand
{
    public int CartId {get;set;}
    public int ProductId {get;set;}
    public int Quantity {get;set;}
}

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.

AddToCart(AddToCartCommand input)
{
     var cart = repository.Carts.First(input.CartId);
     var product = repository.Products.First(input.ProductId);
     cart.Add(product, input.Quantity);

     return RedirectToAction(Request.Referrer);
}

where AddToCartCommand is

class AddToCartCommand
{
    public int CartId {get;set;}
    public int ProductId {get;set;}
    public int Quantity {get;set;}
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文