多个提交按钮中的WebGrid MVC4 [英] Multiple submit buttons in Webgrid MVC4

查看:122
本文介绍了多个提交按钮中的WebGrid MVC4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个在的WebGrid我的索引,并有两个提交按钮来执行单独的操作。

从这个的WebGrid <一个href=\"http://weblogs.asp.net/imranbaloch/archive/2011/09/13/webgrid-helper-with-check-all-checkboxes.aspx\"相对=nofollow> code
如何直接到当按钮被点击后相应的行动。

但是,当我在任何按钮提交,第一个[HttpPost]行动只是调用。
我的code:

I have a WebGrid in My Index and have two submit buttons to perform separate actions.
Webgrid from this code How to direct to the respective actions when the button is clicked on post.
But When I submit on any of the buttons, the first [HttpPost] Action is only called. My code :

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

*的控制器code *

[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");
}

我试图用这个<一个href=\"http://blogs.msdn.com/b/simonince/archive/2011/02/12/detecting-which-button-was-clicked-in-mvc.aspx\"相对=nofollow>链接调用两个动作。但是,指令值不叫。
任何建议。

修改

I tried using this link to call the two actions. But the command value is not called. Any suggestions.
EDIT :

  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();
    }

查看code

<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获取按键值。但它仍然去索引视图点击任何按钮。

有没有在我的code的任何错误。 ?

I used ActionMethodSelectorAttribute for getting the button value. But It still goes to the Index View whichever button is clicked.
Is there any mistake in my code. ?

推荐答案

您应该使用 ActionMethodSelectorAttribute 界面,将识别按钮点击。更多信息请参考以下链接
<一href=\"http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx\" rel=\"nofollow\">http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx

you should use ActionMethodSelectorAttribute interface which will identify which button is click. for more info refer the following link http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx

这篇关于多个提交按钮中的WebGrid MVC4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆