ASP.Net MVC 4与表2的提交按钮/动作 [英] ASP.Net MVC 4 Form with 2 submit buttons/actions
问题描述
我在ASP.Net和剃须刀的形式。
我需要提交两种方式表示形式。上穿过修改
操作,另一个通过验证
行动去。
我应该如何去这样做呢?
我不介意使用JavaScript这一点。
编辑:
使用自定义属性我得到这个错误。
采取行动的当前请求Resultados控制器类型InspecoesController'是下面的操作方法之间暧昧:
System.Web.Mvc.ActionResult Validar(System.Collections.Generic.ICollection<$c$c>1[Waveform.IEP.Intus.Server.Web.ViewModels.ResultadoViewModel])上式Waveform.IEP.Intus.Server.Web.Controllers.InspecoesController
System.Web.Mvc.ActionResult Resultados(System.Collections.Generic.ICollection$c$c>1[Waveform.IEP.Intus.Server.Web.ViewModels.ResultadoViewModel])在类型Waveform.IEP.Intus.Server.Web.Controllers.InspecoesController
块引用>解决方案这就是我们在我们的应用程序:结果
的属性的公共类HttpParamActionAttribute:ActionNameSelectorAttribute
{
公众覆盖布尔IsValidName(ControllerContext controllerContext,串actionName,MethodInfo的MethodInfo的)
{
如果(actionName.Equals(methodInfo.Name,StringComparison.InvariantCultureIgnoreCase))
返回true; VAR请求= controllerContext.RequestContext.HttpContext.Request;
返回请求[methodInfo.Name]!= NULL;
}
}的动作饰吧:的
[HttpParamAction]
公众的ActionResult保存(为MyModel模型)
{
// ...
}[HttpParamAction]
公众的ActionResult发布(为MyModel模型)
{
// ...
}的 HTML /剃须刀的
@using(@ Html.BeginForm())
{
&LT;! - 形式的内容在这里 - &GT;
&LT;输入类型=提交名称=保存值=保存/&GT;
&LT;输入类型=提交名称=发布值=发布/&GT;
}
名称
提交按钮应符合动作/方法的名称属性。这样,您就不必硬在JavaScript code网址
I have a form in ASP.Net and razor.
I need to have two ways of submitting said form. On that goes through the
Edit
action and another that goes through theValidate
action.How should I go about doing this?
I don't mind using JavaScript for this.
EDIT:
Using the custom attribute I get this error.
The current request for action 'Resultados' on controller type 'InspecoesController' is ambiguous between the following action methods: System.Web.Mvc.ActionResult Validar(System.Collections.Generic.ICollection
1[Waveform.IEP.Intus.Server.Web.ViewModels.ResultadoViewModel]) on type Waveform.IEP.Intus.Server.Web.Controllers.InspecoesController System.Web.Mvc.ActionResult Resultados(System.Collections.Generic.ICollection
1[Waveform.IEP.Intus.Server.Web.ViewModels.ResultadoViewModel]) on type Waveform.IEP.Intus.Server.Web.Controllers.InspecoesController
解决方案That's what we have in our applications:
Attributepublic class HttpParamActionAttribute : ActionNameSelectorAttribute { public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo) { if (actionName.Equals(methodInfo.Name, StringComparison.InvariantCultureIgnoreCase)) return true; var request = controllerContext.RequestContext.HttpContext.Request; return request[methodInfo.Name] != null; } }
Actions decorated with it:
[HttpParamAction] public ActionResult Save(MyModel model) { // ... } [HttpParamAction] public ActionResult Publish(MyModel model) { // ... }
HTML/Razor
@using (@Html.BeginForm()) { <!-- form content here --> <input type="submit" name="Save" value="Save" /> <input type="submit" name="Publish" value="Publish" /> }
name
attribute of submit button should match action/method nameThis way you do not have to hard-code urls in javascript
这篇关于ASP.Net MVC 4与表2的提交按钮/动作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!