English 中文(简体)
ASP.Net MVC 3多次提交
原标题:ASP.Net MVC 3 multiple submit

我页面上有一张表格,有两种提交方式。第一个是锚标签,另一个是提交按钮,两者行为不同。

我怎样才能为每一个行动方法指定一个单独的行动方法?谢谢。

最佳回答

这真的取决于锚标签是做什么的 - 假设它触发了提交 Javamart?

如果它实际上只是做一个 GET ,而不是做一个 POST ,那么您可以按@dbaseman 的建议做 -- -- 对两种请求类型有不同的操作方法。

但如果锚确实提交了 Javascript 提交, 那么我更希望只是给提交按钮一个名字, 这样您就可以用一种操作方法在服务器上检测它, 然后从服务器上叉开代码 :

<submit name="fromButtom" value="Submit" />

然后你的动作方法:

public ActionResult Foo(string fromButton)
{
  //if  fromButton  contains  Submit  then you assume it was the button.
}

更好的办法是您可以使用 代替 ,然后您可以将显示的文字与按钮提供的值(如果您重新定位页面可用)脱钩:

<button name="submitMethod" value="fromButton">Submit</button>

现在您可以在动作方法上有一个 < code> submit method 参数, 您可以在其中查找来自按钮 < /code > 的 < code > 参数 。

任意一种方式 - 锁定标记/ javascript (如果您正在这样做的话) 将不提交此值, 因为按钮 s 值仅在单击时才提交 。

问题回答

简单地在您的行为的不同版本中使用 MVC < 坚固 > HttpPost 和 < 坚固 > HttpGet 属性 :

[HttpPost]
public ActionResult FormAction(Model model, string method = post) { ... }

[HttpGet]
public ActionResult FormAction(Model model) { ... }

您可以使用 Url. Action 或 Html. ActionLink http://geekswithblogs.net/liamclenan/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)
{ ... }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签