微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

无法传递两个参数来操作ASP.NET MVC

如何解决无法传递两个参数来操作ASP.NET MVC

我想将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>

解决方法

在后期操作中,只能从主体中获取一个参数,您可以将所有其他参数作为路由参数或查询字符串参数传递给它们,因此您的签名

public ActionResult Index(int id,int quantity)

可能会变成:

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

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

调用该操作

并尝试使用ajax帖子调用操作或提交非超链接的表单

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

,

您使用客户端window.location.href确实没有正确发布消息-这将导致HttpGet。

您应该为商品ID添加一个隐藏的输入,这是相关的代码段:

<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
}

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。