Single to handle multiple "Submit" button Clicks in MVC3 Razor View

Last week, I have faced a problem on handling the multiple "Submit"  buttons clicks in a View.I am not very good at MVC3 but I want to share my experience so that others can find some solution regarding this problem.

Suppose we have many Submit buttons in a View where we want to handle the different functions. In the Controller of the We need to write the Code for different submit buttons as given below

[AcceptVerbs(HttpVerbs.Post)]
        public ActionResult FormActions(string submitButton)
        {
            switch (submitButton.ToLower())
            {
                case "click to know date":
                    this.SetNotification(DateTime.Now.Date.ToString("dd-MMM-yyyy"), 
                     NotificationEnumeration.Success, true);
                    return View("index");
                    break;
                case "click to know time":
                    this.SetNotification(DateTime.Now.ToString ("T"), 
                         NotificationEnumeration.Success, true);
                    return View("index");
                    break;
                default:
                    return View("index");
                    break;
            }
        }
The View Code is given below

@using (Html.BeginForm("FormActions", "Test", FormMethod.Post))
{
    
    <table border="0" cellpadding="0" cellspacing="0">
        <tr>
            <td>
                <input type="submit" name="submitButton" value="Click to Know Time" />
            </td>
        </tr>
        <tr>
            <td>
                <input type="submit" name="submitButton" value="Click to Know Date" />
            </td>
        </tr>
    </table>
    
}


When we click on the "Click to Know Time" we will get the Time and we click on the "Click to Know Date ".
Hope this article is helpful :-)

Popular posts from this blog

Exporting to excel from a custom class object using C#.NET

Get the Clicked Cell Value and Column Name of GridView in ASP.NET

How to apply watermark to the textbox and dropdownlist?