问题
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. ?
回答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
来源:https://stackoverflow.com/questions/13524563/multiple-submit-buttons-in-webgrid-mvc4