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

<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />

从我的控制器动作中,我怎么知道按下了哪个?

有帮助吗?

解决方案

将两个提交按钮命名相同

<input name="submit" type="submit" id="submit" value="Save" />
<input name="submit" type="submit" id="process" value="Process" />

然后在您的控制器中获得提交的价值。只有单击的按钮才能传递其值。

public ActionResult Index(string submit)
{
    Response.Write(submit);
    return View();
}

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

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

    return View();
}

其他提示

<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />

在您的控制器操作中:

public ActionResult SomeAction(string submit)
{
    if (!string.IsNullOrEmpty(submit))
    {
        // Save was pressed
    }
    else
    {
        // Process was pressed
    }
}

这是一个更好的答案,因此我们可以拥有一个按钮的文本和价值:

http://weblogs.asp.net/dfindley/archive/2009/05/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>

和控制器:

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

简而言之,它的提交按钮,但您可以使用名称属性选择名称,因为您对控制器方法参数中的名称或按钮没有义务,您可以按照自己的喜好调用它。

您可以从那里的名称标签识别您的按钮,如下所示,您需要在控制器中进行此类检查

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

这是一种非常简单而简单的方法,可以使用自定义的MultibuttonAttribute易于遵循说明:

http://blog.maartenballiauw.be/post/2009/11/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" /> 

您这样的行动:

[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");
} 

并创建此类:

[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;
    }
}
// Buttons
<input name="submit" type="submit" id="submit" value="Save" />
<input name="process" type="submit" id="process" value="Process" />

// Controller
[HttpPost]
public ActionResult index(FormCollection collection)
{
    string submitType = "unknown";

    if(collection["submit"] != null)
    {
        submitType = "submit";
    }
    else if (collection["process"] != null)
    {
        submitType = "process";
    }

} // End of the index method

为了使变得更容易,我会说您可以将按钮更改为以下内容:

<input name="btnSubmit" type="submit" value="Save" />
<input name="btnProcess" type="submit" value="Process" />

您的控制器:

public ActionResult Create(string btnSubmit, string btnProcess)
{
    if(btnSubmit != null)
       // do something for the Button btnSubmit
    else 
       // do something for the Button btnProcess
}

这篇文章不会回答Coppermill,因为他很久以前就已经回答了。我的帖子将有助于谁将寻求这样的解决方案。首先,我不得不说“ Wduffy的解决方案是完全正确的”,并且它的工作正常,但是我的解决方案(实际上不是我的)将在其他元素中使用,并且它使演示层更偏离控制器(因为您的控制器依赖于控制器用于显示按钮标签的“值”,此功能对于其他语言很重要。)。

这是我的解决方案,给他们不同的名称:

<input type="submit" name="buttonSave" value="Save"/>
<input type="submit" name="buttonProcess" value="Process"/>
<input type="submit" name="buttonCancel" value="Cancel"/>

您必须将按钮的名称指定为以下操作中的参数:

public ActionResult Register(string buttonSave, string buttonProcess, string buttonCancel)
{
    if (buttonSave!= null)
    {
        //save is pressed
    }
    if (buttonProcess!= null)
    {
        //Process is pressed
    }
    if (buttonCancel!= null)
    {
        //Cancel is pressed
    }
}

当用户使用其中一个按钮提交页面时,只有一个参数将具有价值。我想这对他人会有所帮助。

更新

这个答案很老,我实际上重新考虑了我的意见。也许以上解决方案适合将参数传递到模型属性的情况。不要打扰自己,为您的项目采取最佳解决方案。

将两个按钮的名称命名,并从表单中获得检查值。

<div>
   <input name="submitButton" type="submit" value="Register" />
</div>

<div>
   <input name="cancelButton" type="submit" value="Cancel" />
</div>

在控制器侧:

public ActionResult Save(FormCollection form)
{
 if (this.httpContext.Request.Form["cancelButton"] !=null)
 {
   // return to the action;
 }

else if(this.httpContext.Request.Form["submitButton"] !=null)
 {
   // save the oprtation and retrun to the action;
 }
}

在核心2.2剃须刀页面中,此语法有效:

    <button type="submit" name="Submit">Save</button>
    <button type="submit" name="Cancel">Cancel</button>
public async Task<IActionResult> OnPostAsync()
{
    if (!ModelState.IsValid)
        return Page();
    var sub = Request.Form["Submit"];
    var can = Request.Form["Cancel"];
    if (sub.Count > 0)
       {
       .......

您不能使用request.form Collection找出答案吗?如果单击过程,请求。form[“ process”]不会为空

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top