【问题标题】:Can't pass two parameters to action ASP.NET MVC无法将两个参数传递给操作 ASP.NET MVC
【发布时间】:2021-02-14 06:25:05
【问题描述】:

我想将 id 和数量传递给操作,但出现此错误:参数字典包含参数 id 的空条目。

我尝试过地图路由,但找不到正确的方法。

我的行动:

[HttpPost]
public ActionResult Index(int id, int quantity) 
{
     var user = unitOfWork.Users.FindByEmail(HttpContext.User.Identity.Name);
     unitOfWork.Carts.AddProductToCartByEmail(user.Email, id, quantity);
     unitOfWork.Complete();
     return View();
}

我正在尝试从这个页面传递参数:

@using PagedList;
@using PagedList.Mvc;

@model IPagedList<MVC_Task.Models.AllProductsModel>

@{
    ViewBag.Title = "Index";
}

<h2>Products</h2>

<table class="table">
    <tr>
        <th>
            @Html.DisplayNameFor(model => model.First().Name)
        </th>
        <th>
            @Html.DisplayNameFor(model => model.First().Price)
        </th>
        @if (User.Identity.IsAuthenticated)
        {
            <th>
                Quantity
            </th>
        }
    </tr>

    @foreach (var item in Model)
    {
        using (Html.BeginForm("Index", "Product", FormMethod.Post))
        {
            @Html.AntiForgeryToken()
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Price)
                </td>
                @if (User.Identity.IsAuthenticated)
                {
                    <td class="form-inline">
                        @Html.TextBoxFor(modelItem => item.Quantity, new { @type = "number", @class = "form-control" })

                        <input type="submit" value="Add" class="btn btn-default"
                               onclick="window.location.href = '@Url.Action("Index", new { item.Id, item.Quantity})';" /> //on this line I send the parameters
                    </td>
                }
            </tr>
        }
    }
</table>

<center>@Html.PagedListPager(Model, page => Url.Action("Index", new { page }))</center>

【问题讨论】:

    标签: c# asp.net-mvc razor routes


    【解决方案1】:

    在发布动作中,只能从正文中获取一个参数,所有其他参数您可以将它们作为路由参数或查询字符串参数传递,所以您的签名

    public ActionResult Index(int id, int quantity)
    

    可能变成:

    public ActionResult Index(int id, [FromBody] int quantity)
    

    您可以使用 url /index?id=...调用操作

    并尝试使用 ajax post 调用操作或提交表单而不是超链接

    欲了解更多信息,请参阅https://docs.microsoft.com/en-us/aspnet/web-api/overview/formats-and-model-binding/parameter-binding-in-aspnet-web-api#using-frombody

    【讨论】:

    • 关于 ajax 和正文中的单个参数的好建议。
    • 我无法使用此属性。 (Visual Studio 没有为我推荐任何库。
    • @Morin 您也可以使用包装对象(DTO 或数据传输对象/视图模型...)将所有参数组合在一起,那么您不需要 [FromBody] 属性以防万一你找不到它。如果您需要使用 FromBody 属性,您可以使用 System.Web.Http 添加;命名空间
    • 我尝试使用 DTO,但对象在 POST 上为空。
    【解决方案2】:

    使用客户端window.location.href,您并没有真正正确发布 - 这将导致 HttpGet。

    您应该为项目 id 添加一个隐藏输入,这是相关的 sn-p:

    <td class="form-inline">
    
       <!-- add the hidden input -->
       @Html.HiddenFor(modelItem => item.Id)
    
       <!-- keep this as is -->
       @Html.TextBoxFor(modelItem => item.Quantity, new { @type = "number", @class = "form-control" })
    
       <!-- remove the onclick redirect, 
            this will post to the action defined in BeginForm -->
       <input type="submit" value="Add" class="btn btn-default"/>
    </td>
    

    接下来,模型通常作为复杂类型传递。它比简单类型有很多好处,因为你可以扩展它,例如;验证逻辑。

    那么,让我们定义一个模型:

    public class PostModel
    {
       public int Id {get;set;}
       public int Quantity {get;set;}
    }
    

    现在调整您的Index 以接受模型:

    [HttpPost]
    public ActionResult Index(PostModel model) 
    {
       //your logic
    }
    

    默认情况下,绑定器将绑定到模型中的适当属性。如果您的 TextBox 的 nameIdQuantity,它将能够这样做。您可以在呈现的 html 中验证这一点。

    【讨论】:

    • 我试过了,没有帮助。我也有同样的问题。
    • 啊,是的,复杂类型用起来更方便,我来调整一下
    • 我使用了我拥有的AllProductsModels,但它似乎没有正确绑定。我对 ActionResult 没有任何价值。
    • @Morin:你能发布输出的 html,用于 2 文本框和隐藏输入吗?
    • 顺便说一句,我们总是可以默认使用简单的 html 元素,这肯定会起作用
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    • 2012-10-17
    • 1970-01-01
    相关资源
    最近更新 更多