Quantcast
Channel: Andrey Shchekin's Blog - Latest Comments
Viewing all articles
Browse latest Browse all 50

Re: Multiple submit buttons with ASP.NET MVC: final solution

$
0
0

hi Andrey Shchekin,

It did not work for me. Below is the solution

--Controller
========================

public class TestController : Controller
{
public ActionResult Index()
{
return View();
}

[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
// [httpParamAction(Name = "Index", Argument = "cancel")]
public ActionResult Index(string button)
{
return View();
}

[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
// [httpParamAction(Name = "saveDraft", Argument = "saveDraft")]
public ActionResult saveDraft(int a, bool? asHtml)
{
// Do something here
return View();
}

[HttpPost]
[AcceptVerbs(HttpVerbs.Post)]
// [httpParamAction(Name = "publish", Argument = "publish")]
public ActionResult publish(float b)
{
//do something here
return View();
}

public class httpParamActionAttribute : ActionNameSelectorAttribute
{
public string Name { get; set; }

public string Argument { get; set; }
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
{
if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
return true;
if (!actionName.Equals("Index", StringComparison.InvariantCultureIgnoreCase))
return false;
var request = controllerContext.RequestContext.HttpContext.Request;
return request[methodInfo.Name] != null;
}
}
}
}

my view is as below
===================
@model IEnumerable<testmvc.models.testcreatemodel>
@{
ViewBag.Title = "Index";
}

@using (Html.BeginForm("Index", "Test"))
{
<input name="saveDraft" type="submit" value="Save Draft">
<input name="publish" type="submit" value="Publish">
}

If I dont use the attribute [httpParamAction(Name..)], whenever I click on any button, it goes only to below menthod

[HttpPost]
...
public ActionResult Index(string button)
{

}
but if I use this attribute it throws error as below

The current request for action 'Index' on controller type 'TestController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult saveDraft(Int32, System.Nullable`1[System.Boolean]) on type TestMVC.Controllers.TestController
System.Web.Mvc.ActionResult Index(System.String) on type TestMVC.Controllers.TestController
.
I tried all the possible solutions discussed here, but it did not help.
.

.</testmvc.models.testcreatemodel>


Viewing all articles
Browse latest Browse all 50

Trending Articles