Can we have mutiple Submits Button in a View for d

2019-08-21 09:01发布

I have a 4 submit buttons to do different action operation's. For One Submit I have written like this

@using (Html.BeginForm("Edit", "Seller", FormMethod.Post)){ 

}

How can we do for multiple Submit buttons ? Do we need to write the same for every Submit button?

2条回答
女痞
2楼-- · 2019-08-21 09:18

Here is how you do it for two submit buttons, similary you can do this for 'n' submit buttons.

Below is a form with two submit buttons. Note that both these submit buttons have the same name i.e “submitButton”

@Html.BeginForm("MyAction", "MyController"); %>
<input type="submit" name="submitButton" value="Button1" />
<input type="submit" name="submitButton" value="Button2" />
}

Now over to the Controller, the Action takes in an input parameter called string stringButton and the rest is pretty self-explanatory.

public ActionResult MyAction(string submitButton) {
        switch(submitButton) {
            case "Button1":
               // do something here
            case "Button2":
               // do some other thing here
            default:
                // add some other behaviour here
        }
...
}
查看更多
家丑人穷心不美
3楼-- · 2019-08-21 09:33

I'm using MultiButtonAttribute for this use case. It is nice and clear. You can separate the logic into different action methods.

MulitButtonAttribute

[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public class MultiButtonAttribute : ActionNameSelectorAttribute
{
    public MultiButtonAttribute(string matchFormKey) : this(matchFormKey, null) {
    }

    public MultiButtonAttribute(string matchFormKey, string matchFormValue) {
        this.MatchFormKey = matchFormKey;
        this.MatchFormValue = matchFormValue;
    }

    public string MatchFormKey { get; set; }
    public string MatchFormValue { get; set; }

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

In controller

[HttpPost]
[MultiButton("buttonPreviousPage")]
public ActionResult NavigateToPreviousPage(int currentPageIndex)
{
// .. action logic
}

In view

@using (Html.BeginForm("SomeDefaultAction", "MyController", FormMethod.Post)){ 
    <input type="submit" id="buttonPreviousPage" name="buttonPreviousPage" value="Previous" />
    <input type="submit" id="buttonNextPage" name="buttonNextPage" value="Next" />
}

How it works

It is important that MultiButtonAttribute extends ActionNameSelectorAttribute. When MVC is choosing the right method to match againts the route and it will find such an attribute on method, it will call the method IsValidName on attribute passing the ControllerContext. Than it is looking if the key (button name) is in the POSTed form, having not null value (or eventually you can define the expected value of key(button), but it is not necessary).

Works pretty well in MVC 2 (but I hope it will work for later versions). Another advantage is that you are able to localize the values of buttons.

查看更多
登录 后发表回答