HTTP POST
Boa noite‼
Estou trabalhando em um projeto MVC3 e preciso ter dois httppost num mesmo controller, cada um para sua view. Como posso fazer isso?
vlw.
Estou trabalhando em um projeto MVC3 e preciso ter dois httppost num mesmo controller, cada um para sua view. Como posso fazer isso?
vlw.
Dando uma boa pesquisada na net achei a soluçao.
public class HttpParamActionAttribute : ActionNameSelectorAttribute {
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) {
if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
return true;
if (!actionName.Equals([Ô]Action[Ô], StringComparison.InvariantCultureIgnoreCase))
return false;
var request = controllerContext.RequestContext.HttpContext.Request;
return request[methodInfo.Name] != null;
}
}
How to use it? Just have a form similar to this:
<% using (Html.BeginForm([Ô]Action[Ô], [Ô]Post[Ô])) { %>
<!— …form fields… -->
<input type=[Ô]submit[Ô] name=[Ô]saveDraft[Ô] value=[Ô]Save Draft[Ô] />
<input type=[Ô]submit[Ô] name=[Ô]publish[Ô] value=[Ô]Publish[Ô] />
<% } %>
and controller with two methods
public class PostController : Controller {
[HttpParamAction]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveDraft(…) {
//…
}
[HttpParamAction]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Publish(…) {
//…
}
}
public class HttpParamActionAttribute : ActionNameSelectorAttribute {
public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) {
if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase))
return true;
if (!actionName.Equals([Ô]Action[Ô], StringComparison.InvariantCultureIgnoreCase))
return false;
var request = controllerContext.RequestContext.HttpContext.Request;
return request[methodInfo.Name] != null;
}
}
How to use it? Just have a form similar to this:
<% using (Html.BeginForm([Ô]Action[Ô], [Ô]Post[Ô])) { %>
<!— …form fields… -->
<input type=[Ô]submit[Ô] name=[Ô]saveDraft[Ô] value=[Ô]Save Draft[Ô] />
<input type=[Ô]submit[Ô] name=[Ô]publish[Ô] value=[Ô]Publish[Ô] />
<% } %>
and controller with two methods
public class PostController : Controller {
[HttpParamAction]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult SaveDraft(…) {
//…
}
[HttpParamAction]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Publish(…) {
//…
}
}
http://blog.ashmind.com/2010/03/15/multiple-submit-buttons-with-asp-net-mvc-final-solution/
http://www.dotnetcurry.com/ShowArticle.aspx?ID=724
http://www.dotnetcurry.com/ShowArticle.aspx?ID=724
Tópico encerrado , respostas não são mais permitidas