How do you handle multiple submit buttons in ASP.NET MVC Framework?

后端 未结 30 3087
一个人的身影
一个人的身影 2020-11-21 07:16

Is there some easy way to handle multiple submit buttons from the same form? For example:

<% Html.BeginForm(\"MyAction\", \"MyController\", FormMethod.Pos         


        
30条回答
  •  时光取名叫无心
    2020-11-21 07:43

    I've created an ActionButton method for the HtmlHelper. It will generate normal input button with a bit of javascript in the OnClick event that will submit the form to the specified Controller/Action.

    You use the helper like that

    @Html.ActionButton("MyControllerName", "MyActionName", "button text")
    

    this will generate the following HTML

    
    

    Here is the extension method code:

    VB.Net

    
    Function ActionButton(pHtml As HtmlHelper, pAction As String, pController As String, pRouteValues As Object, pBtnValue As String, pBtnName As String, pBtnID As String) As MvcHtmlString
        Dim urlHelperForActionLink As UrlHelper
        Dim btnTagBuilder As TagBuilder
    
        Dim actionLink As String
        Dim onClickEventJavascript As String
    
        urlHelperForActionLink = New UrlHelper(pHtml.ViewContext.RequestContext)
        If pController <> "" Then
            actionLink = urlHelperForActionLink.Action(pAction, pController, pRouteValues)
        Else
            actionLink = urlHelperForActionLink.Action(pAction, pRouteValues)
        End If
        onClickEventJavascript = "this.form.action = '" & actionLink & "'; this.form.submit();"
    
        btnTagBuilder = New TagBuilder("input")
        btnTagBuilder.MergeAttribute("type", "button")
    
        btnTagBuilder.MergeAttribute("onClick", onClickEventJavascript)
    
        If pBtnValue <> "" Then btnTagBuilder.MergeAttribute("value", pBtnValue)
        If pBtnName <> "" Then btnTagBuilder.MergeAttribute("name", pBtnName)
        If pBtnID <> "" Then btnTagBuilder.MergeAttribute("id", pBtnID)
    
        Return MvcHtmlString.Create(btnTagBuilder.ToString(TagRenderMode.Normal))
    End Function
    

    C# (the C# code is just decompiled from the VB DLL, so it can get some beautification... but time is so short :-))

    public static MvcHtmlString ActionButton(this HtmlHelper pHtml, string pAction, string pController, object pRouteValues, string pBtnValue, string pBtnName, string pBtnID)
    {
        UrlHelper urlHelperForActionLink = new UrlHelper(pHtml.ViewContext.RequestContext);
        bool flag = Operators.CompareString(pController, "", true) != 0;
        string actionLink;
        if (flag)
        {
            actionLink = urlHelperForActionLink.Action(pAction, pController, System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue(pRouteValues));
        }
        else
        {
            actionLink = urlHelperForActionLink.Action(pAction, System.Runtime.CompilerServices.RuntimeHelpers.GetObjectValue(pRouteValues));
        }
        string onClickEventJavascript = "this.form.action = '" + actionLink + "'; this.form.submit();";
        TagBuilder btnTagBuilder = new TagBuilder("input");
        btnTagBuilder.MergeAttribute("type", "button");
        btnTagBuilder.MergeAttribute("onClick", onClickEventJavascript);
        flag = (Operators.CompareString(pBtnValue, "", true) != 0);
        if (flag)
        {
            btnTagBuilder.MergeAttribute("value", pBtnValue);
        }
        flag = (Operators.CompareString(pBtnName, "", true) != 0);
        if (flag)
        {
            btnTagBuilder.MergeAttribute("name", pBtnName);
        }
        flag = (Operators.CompareString(pBtnID, "", true) != 0);
        if (flag)
        {
            btnTagBuilder.MergeAttribute("id", pBtnID);
        }
        return MvcHtmlString.Create(btnTagBuilder.ToString(TagRenderMode.Normal));
    }
    

    These methods have various parameters, but for the ease of use you can create some overload that take just the parameters you need.

提交回复
热议问题