0

我的索引中有一个 WebGrid,并且有两个提交按钮来执行单独的操作。来自此代码
的 Webgrid 如何在单击发布按钮时定向到相应的操作。 但是当我提交任何按钮时,只会调用第一个 [HttpPost] 操作。我的代码:

@using (Html.BeginForm())
{
    <fieldset>
    <div id="CheckBoxDiv">
        @grid.GetHtmlWithSelectAllCheckBox(
            tableStyle: "grid", 
            checkBoxValue: "ProductID",
            columns: grid.Columns(
                grid.Column(columnName: "ProductName"),
                grid.Column(columnName: "Quantity"),
                grid.Column(" ", " ", format: @<a href="@Url.Action("Index", "Edit", new { UserID = "12345", partnerid = "111" })">Edit</a>),
                grid.Column(columnName: "Rate"),
                grid.Column(columnName: "Price")                  
            )
         ) 
    </div>
    <p>   
         <input type="image" class="btn" name="command" value="Checkout" src="~/Images/Checkout.PNG" style="width: 62px; height: 25px;"  />
         <input type="image" class="btn" name="command" value="Delete" src="~/Images/Remove.png" style="width: 62px; height: 25px;"  />
    </p>
    </fieldset>
}

*控制器代码 *

[HttpPost]   
public ActionResult Index(string[] selectedRows)
{
    for (int i = 0; i < selectedRows.Length; i++)
    {
        var objselected = selectedRows.ToList();
        SqlCommand scmd = new SqlCommand("SpCheckDetails", con);
        scmd.CommandType = CommandType.StoredProcedure;
        scmd.Parameters.AddWithValue("@userid", id);
        scmd.Parameters.AddWithValue("@partnerID", partnerid);
        scmd.Parameters.AddWithValue("@productID", objselected);
        scmd.ExecuteNonQuery();
     }
     return RedirectToAction("Index","Checkout");
 }

[HttpPost]       
public ActionResult Delete(string UserID, string partnerid, string productid)
{
     return RedirectToAction("Index", "Cart");
}

我尝试使用此链接来调用这两个操作。但是没有调用命令值。有什么建议么。
编辑 :

  public ActionResult Index()
        {
            string id = "12345";
            string partnerid = "xxxxx";
            return View(_service.GetCartDetails(id,partnerid));
        }
    [AcceptVerbs(HttpVerbs.Post)]
    [AcceptParameter(Name = "Checkout")]
    public ActionResult Index(string[] selectedRows, string button)
    {
            return View();
    }
    [AcceptVerbs(HttpVerbs.Post)]
    [AcceptParameter(Name = "Delete")]
    public ActionResult Delete(string[] selectedRows,string UserID, string partnerid, string productid)
    {
            return View();
    }

查看代码

<input type="image" class="btn" name="Checkout" value="Checkout" src="~/Images/Checkout.PNG"/>
<input type="image" class="btn" name="Delete" value="Delete" src="~/Images/Remove.png" />

我使用 ActionMethodSelectorAttribute 来获取按钮值。但无论单击哪个按钮,它仍然会转到索引视图。
我的代码中是否有任何错误。?

4

1 回答 1

1

您应该使用ActionMethodSelectorAttribute接口,该接口将识别单击哪个按钮。有关更多信息,请参阅以下链接 http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx

于 2012-11-23T07:48:32.890 回答