已按下提交按钮的MVC

Cop*_*ill 123 c# asp.net asp.net-mvc razor

我的MVC表单上有两个按钮:

<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />
Run Code Online (Sandbox Code Playgroud)

从我的Controller动作我怎么知道哪一个被按下了?

WDu*_*ffy 165

将提交按钮命名为相同

<input name="submit" type="submit" id="submit" value="Save" />
<input name="submit" type="submit" id="process" value="Process" />
Run Code Online (Sandbox Code Playgroud)

然后在你的控制器中获取提交的值.只有单击的按钮才会传递其值.

public ActionResult Index(string submit)
{
    Response.Write(submit);
    return View();
}
Run Code Online (Sandbox Code Playgroud)

您当然可以评估该值以使用开关块执行不同的操作.

public ActionResult Index(string submit)
{
    switch (submit)
    {
        case "Save":
            // Do something
            break;
        case "Process":
            // Do something
            break;
        default:
            throw new Exception();
            break;
    }

    return View();
}
Run Code Online (Sandbox Code Playgroud)

  • 注意本地化可能给这个解决方案带来的问题,Darin的解决方案对此无法解决. (13认同)

Dar*_*rov 46

<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />
Run Code Online (Sandbox Code Playgroud)

在你的控制器动作中:

public ActionResult SomeAction(string submit)
{
    if (!string.IsNullOrEmpty(submit))
    {
        // Save was pressed
    }
    else
    {
        // Process was pressed
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 我想只需将其添加到参数列表并正确命名即可添加更多按钮。很好的解决方案! (2认同)

Yak*_*nor 33

这是一个更好的答案,因此我们可以为按钮提供文本和值:

http://weblogs.asp.net/dfindley/archive/2009/05/31/asp-net-mvc-multiple-buttons-in-the-same-form.aspx

</p>
<button name="button" value="register">Register</button>
<button name="button" value="cancel">Cancel</button>
</p>
Run Code Online (Sandbox Code Playgroud)

和控制器:

public ActionResult Register(string button, string userName, string email, string password, string confirmPassword)
{
if (button == "cancel")
    return RedirectToAction("Index", "Home");
...
Run Code Online (Sandbox Code Playgroud)

简而言之,它是一个SUBMIT按钮,但你选择使用name属性的名称,它更强大,因为你没有义务提交控制器方法参数中的名称或按钮,你可以随意调用它...


Kin*_*hil 18

你可以从下面的名称标签识别你的按钮,你需要在你的控制器中检查这样

if (Request.Form["submit"] != null)
{
//Write your code here
}
else if (Request.Form["process"] != null)
{
//Write your code here
}
Run Code Online (Sandbox Code Playgroud)


Owe*_*wen 5

这是一个非常好用且简单的方法,使用自定义MultiButtonAttribute非常容易理解:

http://blog.maartenballiauw.be/post/2009/11/26/Supporting-multiple-submit-buttons-on-an-ASPNET-MVC-view.aspx

总结一下,按照以下方式设置提交按钮:

<input type="submit" value="Cancel" name="action" />
<input type="submit" value="Create" name="action" /> 
Run Code Online (Sandbox Code Playgroud)

你的行为是这样的:

[HttpPost]
[MultiButton(MatchFormKey="action", MatchFormValue="Cancel")]
public ActionResult Cancel()
{
    return Content("Cancel clicked");
}

[HttpPost]
[MultiButton(MatchFormKey = "action", MatchFormValue = "Create")]
public ActionResult Create(Person person)
{
    return Content("Create clicked");
} 
Run Code Online (Sandbox Code Playgroud)

并创建此类:

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultiButtonAttribute : ActionNameSelectorAttribute
{
    public string MatchFormKey { get; set; }
    public string MatchFormValue { get; set; }

    public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
    {
        return controllerContext.HttpContext.Request[MatchFormKey] != null &&
            controllerContext.HttpContext.Request[MatchFormKey] == MatchFormValue;
    }
}
Run Code Online (Sandbox Code Playgroud)