MVC2尝试基于具有动态名称的文本框将多个参数传递给ActionResult

发布于 2024-11-16 10:37:56 字数 1316 浏览 5 评论 0原文

我正在尝试为添加到购物车的商品数量实现数量选择器。

我有一个文本框,其中包含目录中每行项目的动态名称。每行都有一个“添加到购物车”按钮。

如果我在文本框中输入所需的数量并单击“添加到购物车”按钮,我希望将所选商品的输入数量添加到我的购物车。

将新数量添加到购物车数据库的控制器操作如下:

public ActionResult AddToCart(int productID, int quant)
{
    repository.AddItemToOrder(productID, quant);
    return RedirectToAction("Browse");
}

我知道调用“浏览”操作来再次呈现页面并不是最有效的方法。我稍后会解释这一点。

我的问题是:如何使用两个参数调用此“AddToCart”控制器操作?

我能够得到一个使用 1 个参数的版本。在这种情况下,上述控制器操作中的第二个参数 quant 被删除。

我的视图中的行是:

<input type="button" onclick="document.location.href = '<%: Url.Action("AddToCart") %>'+'?productID=<%: item.ProductID %>'  " value="Add to Cart" />

虽然这有效,但我需要能够引用数量的文本框。

我尝试了下一行,其中在我的视图中包含多个参数:

<input type="button" onclick="document.location.href = '<%: Url.Action("AddToCart") %>'+'?productID=<%: item.ProductID %>'+'?quant=<%: item.ProductID %>'  " value="Add to Cart" />

分配给“quant”的值与分配给“productID”的值相同。我这样做只是为了让某些东西正常工作。一旦它工作,我计划使用具有动态名称的文本框的值。 (我也需要一些帮助)

当我测试并单击该行的按钮时,收到以下错误:

参数字典包含不可为空类型“System.Int32”的参数“productID”的空条目“OER.Controllers.eCommerceController”中的“System.Web.Mvc.ActionResult AddToCart2(Int32, Int32)”方法。可选参数必须是引用类型、可为 null 的类型,或者声明为可选参数。 参数名称:参数

I am trying to implement a quantity selector for the number of items added to a shopping cart.

I have a textbox with a dynamic name for each row of items in the catalog. Each row has a "Add to Cart" button.

If I enter the desired quantity in the textbox and click the "Add To Cart" button, I want the entered quantity of the selected item to be added to my Cart.

The controller action that adds the new quantity to the database for the cart is as follows:

public ActionResult AddToCart(int productID, int quant)
{
    repository.AddItemToOrder(productID, quant);
    return RedirectToAction("Browse");
}

I know that calling the "Browse" action to render the page again is not the most efficient method. I will account for that later.

My question is: How do I make a call to this "AddToCart" controller action with both parameters?

I was able to get a version working with 1 parameter. In this case the second parameter in the above controller action, quant, was removed.

The line in my View was:

<input type="button" onclick="document.location.href = '<%: Url.Action("AddToCart") %>'+'?productID=<%: item.ProductID %>'  " value="Add to Cart" />

While this worked, I need to be able to reference the textbox for the quantity.

I tried this next line that includes multiple parameters in my View:

<input type="button" onclick="document.location.href = '<%: Url.Action("AddToCart") %>'+'?productID=<%: item.ProductID %>'+'?quant=<%: item.ProductID %>'  " value="Add to Cart" />

The value assigned to "quant" is the same as what is assigned to "productID". I did this to simply get something to work correctly. Once it is working I plan to use the value of the textbox that has a dynamic name. (I need some help with that as well)

When I tested and clicked the button for the row, I received the following error:

The parameters dictionary contains a null entry for parameter 'productID' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult AddToCart2(Int32, Int32)' in 'OER.Controllers.eCommerceController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.
Parameter name: parameters

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

眉黛浅 2024-11-23 10:37:56

您需要在查询字符串中的变量之间使用 & 符号 &,而不是另一个 ?...简单的拼写错误。

/AddToCart?productId=3&quant=1

如果您想给 quant 一个默认值,因此并不总是需要,您可以将操作方法​​更改为类似以下内容:

public ActionResult AddToCart(int productID, int quant = 1)

根据您的评论,我将设置一个按钮将提交的表单,而不是将其全部压缩到 onclick 中:

@using(Html.BeginForm("AddToCart")) {
    @Html.Hidden("productId", item.ProductId)
    @Html.Input("quant")
    <input type="submit" value="Add to Cart" />
}

如果您只想切换到常规链接而不是按钮,则需要连接一些 jQuery 来拉动 quant单击文本框中的代码> 参数。

$('.addToCartLink').click(function() {  // All the "add to cart" links would have this class
    // Pull the product ID (just an example)
    var productId = $(this).attr('data-id');
    // Set the quantity
    var quantity = $('#txtlinequantity-'+productId).val();
    // Possible validation here
    ...
    // Hit the action method
    window.location = '/AddToCart?productId='+productId+'&quant='+quantity;
});

You need an ampersand, &, between variables in a querystring, not another ?... simple typo.

/AddToCart?productId=3&quant=1

If you want to give quant a default value, so it is not always required, you can change your action method to something like:

public ActionResult AddToCart(int productID, int quant = 1)

Based on your comments, I would set up a form that the button will submit, instead of squeezing it all into an onclick:

@using(Html.BeginForm("AddToCart")) {
    @Html.Hidden("productId", item.ProductId)
    @Html.Input("quant")
    <input type="submit" value="Add to Cart" />
}

If you want to switch to just a regular link instead of a button, you'll need to wire up some jQuery to pull the quant parameter from the text box on click.

$('.addToCartLink').click(function() {  // All the "add to cart" links would have this class
    // Pull the product ID (just an example)
    var productId = $(this).attr('data-id');
    // Set the quantity
    var quantity = $('#txtlinequantity-'+productId).val();
    // Possible validation here
    ...
    // Hit the action method
    window.location = '/AddToCart?productId='+productId+'&quant='+quantity;
});
-残月青衣踏尘吟 2024-11-23 10:37:56

你不想

在您提交的表单中?

如果您提交了一个表单元素,MVC 将自动提取所需的信息,并根据 name 字段(或 id)将其分配给代码隐藏中的等效变量。 value 字段是分配给相应变量的值。

Don't you want

<input type="button" value="<%: item.ProductID %>" name="productID" id="productID"/>

inside the form you're submitting?

If you have a form element being submitted, MVC will automatically pull the required information and assign it to the equivalent variable in your code behind based on the name field (or id). The value field is the value to assign to the corresponding variable.

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