0

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

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

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

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" />

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. ?

kk1076
  • 1,740
  • 13
  • 45
  • 76

1 Answers1

1

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

Shivkumar
  • 1,903
  • 5
  • 21
  • 32
  • I tried using the ActionMethodSelectorAttribute. But both the actions are not called separately. In the example, they used buttons to submit. But In my case, I am using two images for submit. Will that be the mistake ?? – kk1076 Nov 26 '12 at 07:05
  • no have you given different name to both submit button?, it would be better if show me some code snap so that i will able find out what exact issue. – Shivkumar Nov 26 '12 at 11:32
  • since I used the input type as image, the submit is not going to the respective action. so I changed my input type as button and got the output. – kk1076 Nov 26 '12 at 12:32
  • ohhh it's because "IsValidForRequest" method you have taken button name from Form Collection that's why it's work.. – Shivkumar Nov 26 '12 at 12:39