带有 2 个提交按钮/操作的 ASP.Net MVC 4 表单 [英] ASP.Net MVC 4 Form with 2 submit buttons/actions
问题描述
我在 ASP.Net 和 razor 中有一个表单.
I have a form in ASP.Net and razor.
我需要通过两种方式提交上述表单:一种通过 Edit
操作,另一种通过 Validate
操作.
I need to have two ways of submitting said form: one that goes through the Edit
action, and another that goes through the Validate
action.
我应该怎么做?
我不介意为此使用 JavaScript.
I don't mind using JavaScript for this.
使用自定义属性时出现此错误.
Using the custom attribute I get this error.
当前对控制器类型InspecoesController"的操作Resultados"的请求在以下操作方法之间不明确:System.Web.Mvc.ActionResult Validar(System.Collections.Generic.ICollection1[Waveform.IEP.Intus.Server.Web.ViewModels.ResultadoViewModel]) 类型 Waveform.IEP.Intus.Server.Web.Controllers.检查控制器System.Web.Mvc.ActionResult Resultados(System.Collections.Generic.ICollection
1[Waveform.IEP.Intus.Server.Web.ViewModels.ResultadoViewModel]) 类型 Waveform.IEP.Intus.Server.Web.Controllers.InspecoesController
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:
Attribute
public 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;
}
}
用它装饰的动作:
[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
提交按钮的属性应该匹配动作/方法名称
name
attribute of submit button should match action/method name
这样你就不必在 javascript 中硬编码 url
This way you do not have to hard-code urls in javascript
这篇关于带有 2 个提交按钮/操作的 ASP.Net MVC 4 表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!