回答
它确实取决于锚标记的作用 - 大概是它触发在JavaScript中提交?
如果它实际上只是在执行GET
而不是,那么您可以按照@dbaseman的建议 - 为两种请求类型分别设置操作方法。
但是,如果锚没有JavaScript的提交,然后我的偏好是简单地给提交按钮的名称,以便您可以检测到它的一个操作方法在服务器上,然后用叉子叉从那里代码:
<submit name="fromButtom" value="Submit" />
然后你的操作方法:
public ActionResult Foo(string fromButton)
{
//if 'fromButton' contains 'Submit' then you assume it was the button.
}
更妙的是你可以使用一个<button>
代替,然后你就可以从价值离婚显示的文本,该按钮提交(如果你是本地化的页面是有用的) :
<button name="submitMethod" value="fromButton">Submit</button>
现在,您可以在您的操作方法上有一个submitMethod
参数,其中您寻找'fromButton'
。
无论哪种方式 - 锚定标记/ JavaScript(如果这是你如何做)不会提交此值,因为按钮的值只在点击时提交。
好,我可以使用这个解决方案,但是我使用提交按钮中的值作为我的显示文本,我不想在我的代码中依赖它。我使用的是一个自定义的HTML帮手,如下所示 – ancdev
只需使用MVC HttpPost和HTTPGET在不同版本的动作的属性:
[HttpPost]
public ActionResult FormAction(Model model, string method = post) { ... }
[HttpGet]
public ActionResult FormAction(Model model) { ... }
您可能想要在这里更改方法名称 - 不会编译。你需要不同的方法名称和一个'[ActionName]':) –
@AndrasZoltan除非我失去它(完全可能,因为它是凌晨2点),应该complie。你看到什么问题? – McGarnagle
哈哈 - 我去过那儿了!两种方法具有相同的名称和相同的参数;即他们是重复:) –
您可以使用Url.Action或Html.ActionLink http://geekswithblogs.net/liammclennan/archive/2008/05/21/122298.aspx。
如果这些方法完全不同,那么您应该使用JavaScript来使它们的行为不同。使用jQuery例如,它看起来像:
$("#sbutton").click(function() {
alert("Submit button clicked!");
// Do whatever you need - validate/initialize-fields/etc.
return false; // This is required to prevent normal form submit
});
$("#slink").click(function() {
alert("Link clicked!");
// Do whatever you need - validate/initialize-fields/etc.
return false; // This is required to prevent normal form submit
});
我想补充另一种解决办法,如果你想创造的东西,将与JavaScript的工作,关闭,这不是方便更改按钮的名称和值,那么你可以试试这是我在生产代码中使用的。
创建这样一个帮手:
using System;
using System.Reflection;
using System.Web.Mvc;
namespace Whatever
{
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultipleSubmitAttribute : ActionNameSelectorAttribute
{
public string Name { get; set; }
public string Argument { get; set; }
public override bool IsValidName(ControllerContext controllerContext,
string actionName, MethodInfo methodInfo)
{
bool isValidName = false;
string keyValue = string.Format("{0}:{1}", Name, Argument);
var value = controllerContext.Controller.ValueProvider.GetValue(keyValue);
if (value != null)
{
value = new ValueProviderResult(Argument, Argument, null);
controllerContext.Controller.ControllerContext.RouteData.Values[Name] = Argument;
isValidName = true;
}
return isValidName;
}
}
}
现在在你的表格,你可以做这样的事情:
<input type="submit" name="action:DoSomething" value="Do something" />
<input type="submit" name="action:DoSomethingElse" value="Do something else" />
然后在你的控制器,你可以装饰你这样的操作方法:
[HttpPost]
[MultipleSubmit(Name = "action", Argument = "DoSomething")]
public ActionResult DoSomething(ViewModel vm)
{ ... }
[HttpPost]
[MultipleSubmit(Name = "action", Argument = "DoSomethingElse")]
public ActionResult DoSomethingElse(ViewModel vm)
{ ... }
如果我有两个按钮,而不是标签和一个按钮 – ancdev
这个解决方案本来可以工作的很好,我认为它可能会派上用场,以备将来使用。另外,你也可以使用javascript这样的''标签来提交它:'onclick =“$(this).parents('form')。submit(); return false;'' –
- 1. 表单提交多次asp.net mvc
- 2. ASP.NET MVC 3型空对提交
- 3. 多次提交按钮(asp.net mvc3)
- 4. 通过.ascx&ajax加载的ASP.NET MVC表单多次提交
- 5. 如何避免在ASP.NET MVC中多次提交表单?
- 6. 通过ajax加载的ASP.NET MVC表单多次提交
- 7. 停止cakephp 3.x FORM在多点提交后多次提交提交按钮
- 8. 在MVC中防止多次提交
- 9. MVC asp.net:提交多个表单
- 10. ASP.NET MVC:多使用提交Ajax.BeginForm
- 11. 多次提交
- 12. 多次提交
- 13. MVC 3 Ajax.BeginForm提交问题
- 14. 表单提交两次mvc
- 15. ASP.NET MVC提交表格
- 16. ASP.NET MVC表单提交
- 17. ajax表格在第二次提交时提交两次,第三次提交时提交3次等
- 18. asp.net表单提交两次
- 19. 在asp.net mvc中使用Valums插件提交的Ajax表单提交3
- 20. ASP.net MVC 3多选列表
- 21. ASP.Net MVC 3个多CheckBoxLists
- 22. ASP.NET MVC 3多语言SEO
- 23. 通过点击两次提交避免在Asp.net MVC中重复表单提交
- 24. jQuery Ajax多次提交多次
- 25. Asp.net mvc ajax多跑一次
- 26. 如何防止ASP.NET MVC 3中的重复表单提交?
- 27. Asp.Net MVC 3 formcollection提交后丢失数据
- 28. 简单的JQuery表单提交与ASP.NET MVC 3
- 29. ASP.NET MVC 3不显眼的验证,提交和TinyMCE
- 30. 提交使用jquery ajax的asp.net MVC 3表单
您可以在提交之前使用javascript更改操作方法。 –