MVC razor form with multiple different submit buttons?

前端 未结 13 1996
-上瘾入骨i
-上瘾入骨i 2020-12-04 12:59

A Razor view has 3 buttons inside a form. All button\'s actions will need form values which are basically values coming input fields.

Every time I click any of butto

13条回答
  •  执念已碎
    2020-12-04 13:43

    You could use normal buttons(non submit). Use javascript to rewrite (at an 'onclick' event) the form's 'action' attribute to something you want and then submit it. Generate the button using a custom helper(create a file "Helper.cshtml" inside the App_Code folder, at the root of your project) .

    @helper SubmitButton(string text, string controller,string action)
    {   
        var uh = new System.Web.Mvc.UrlHelper(Context.Request.RequestContext);
        string url = @uh.Action(action, controller, null);   
        
    }
    

    And then use it as:

    @Helpers.SubmitButton("Text for 1st button","ControllerForButton1","ActionForButton1")
    @Helpers.SubmitButton("Text for 2nd button","ControllerForButton2","ActionForButton2")
    ...
    

    Inside your form.

提交回复
热议问题