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

后端 未结 30 2988
一个人的身影
一个人的身影 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:35

    My JQuery approach using an extension method:

    public static MvcHtmlString SubmitButtonFor(this HtmlHelper helper, Expression> action, string value) where TController : Controller
    {
        RouteValueDictionary routingValues = Microsoft.Web.Mvc.Internal.ExpressionHelper.GetRouteValuesFromExpression(action);
    
        var onclick = string.Format("$('form').first().attr('action', '/{0}')", string.Join("/", routingValues.Values.ToArray().Where(x => x != null).Select(x => x.ToString()).ToArray()));
        var html = "";
    
        return MvcHtmlString.Create(html);
    }
    

    You can use it like this:

    @(Html.SubmitButtonFor(c => c.Save(null), "Save"))
    

    And it renders like this:

    
    

提交回复
热议问题