Dot Net Tricks

Articles about .NET, ASP.NET, C#, Object Oriented Programming and Agile Methodologies
Welcome to Dot Net Tricks Sign in | Join | Help
in Search

Software Theosophy

ASP.NET MVC - Multiple buttons on a single form.

I haven't posted in a while do to being busy with my new job, but I thought I'd just share this code snippet. 

In the new asp.net mvc framework, you often have the need for a single form, but multiple buttons that each post to a different controller action.  In webforms you didn't have to worry about this as all forms did a postback and asp.net created event wiring magic to wire up each button to an event handling method.  But with MVC, we're forced to go a little old school.  So I created a little helper method that I'll probably submit to MvcContrib or something if they don't have one like this already.  It lets you wire up controller methods to each submit button in a strongly typed fashion using lambda expresssions. 

Here's an example:

   <% using (Html.BeginForm())
      { %>
  
    <h2>About</h2>
    <p>
        Put content here.       
        <%= Html.SubmitButton<HomeController>(x => x.DoSomething(), "Do something now")%>
        <%= Html.SubmitButton<HomeController>(x => x.DoSomethingElse(), "Do something else")%>
    </p>
    <% } %>

Obviously the generic parameter above called "HomeController" is the controller class and the DoSomething() is the method in that class.  I also let you specify the button label and optionally the name of the button and other html attributes.

Here's the source code:

    public static class SubmitHelper
    {
        public static string SubmitButton<TController>(this HtmlHelper helper, Expression<Action<TController>> action, string buttonText) where TController : Controller
        {           
            var name = buttonText.Replace(" ", ":");
            name = "SUBMIT_BUTTON:" + name;
            return SubmitButton<TController>(helper, action, name, buttonText);
        }

        public static string SubmitButton<TController>(this HtmlHelper helper, Expression<Action<TController>> action, string name, string buttonText, IDictionary<string,object> dictionary) where TController : Controller
        {           
            return helper.SubmitButton(name, buttonText, dictionary  );
        }

        public static string SubmitButton<TController>(this HtmlHelper helper, Expression<Action<TController>> action, string name, string buttonText) where TController : Controller
        {
            var link =  LinkBuilder.BuildUrlFromExpression<TController>(helper.ViewContext.RequestContext, helper.RouteCollection, action);
              var dictionary = new Dictionary<string,object>();
              dictionary.Add("OnClick", "this.form.action = '" + link + "';");
            return helper.SubmitButton(name, buttonText, dictionary);
                       
        }

    }

For this to build, you must reference the Microsoft.Web.Mvc dll and namespace, which can be found here:
http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471

or click below to download directly:
http://aspnet.codeplex.com/Release/ProjectReleases.aspx?ReleaseId=24471#DownloadId=61773

Enjoy!
Published Wednesday, May 06, 2009 3:08 PM by Fregas
Filed Under: ,
Anonymous comments are disabled

About Fregas

Craig is currently the Lead Developer in Fort Worth, Texas for Enilon Group, a web development firm. He has been programming since 3rd grade (using the Commodoore PET) and professionally for the past 7 years. He has written several articles for ASPToday.com and co-authored the book "Beginning Web Programming using VB.NET and Visual Studio .NET" Currently, his favorite programming language is C#, but he has programmed in Visual Basic, T-SQL, Ruby, ColdFusion, ASP 3.0/VBScript, ASP.NET, Javascript, Java and even Pascal. Besides programming, Craig is best known for his cooking and his somewhat offbeat sense of humor.

This Blog

Post Calendar

<May 2009>
SuMoTuWeThFrSa
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

Syndication

Powered by Community Server, by Telligent Systems