Use of Buttons command in MVC application

In this article we submit a Login form without using a scripting language (like javascript or jquery) in an MVC application. Here we use multiple buttons in the view page, Then see how it will go to the controller on button click event.

Let's create an application in an MVC. Then create a database and then create a table. Then add an entity framework model select your database at creating them in your Model folder of your application. Then save it.

Now add a controller and add a view page for it. In view page, we add 3 submit buttons. Write this below code in your view page.

Source Code:

@model MultiButtonMVC.Models.Login

@{
    ViewBag.Title = "Load";
}

<h2>
    Handling multiple submit buttons in MVC
</h2>
<h5 style="color: Red">@ViewBag.msg</h5>
<form action="Load" id="myform" method="post">
    <table>
        <tr>
            <td>
                UserName
            </td>
            <td>
                :
            </td>
            <td>
                @Html.TextBoxFor(m => m.userName)
            </td>
            <td>
                @Html.ValidationMessageFor(m => m.userName)
            </td>
        </tr>
        <tr>
            <td>
                Password
            </td>
            <td>
                :
            </td>
            <td>
                @Html.TextBoxFor(m => m.password)
            </td>
            <td>
                @Html.ValidationMessageFor(m => m.password)
            </td>
        </tr>
    </table>
    <br />

    <div style="padding-left: 80px;">
        <input type="submit" id="Login" value="Login" name="Command" title="Login" />
        <input type="submit" id="Register" value="Register" name="Command" title="Register" />
        <input type="submit" value="Cancel" name="Command" title="Cancel" />

    </div>
</form>

@model MultiButtonMVC. Models. Login is used for model class reference. Here the "MultiButtonMVC" is my application name, "Models" is my folder name which is present in my application, Then "Login" is my model class name this class file comes from your model context file.

 In the view page, we mention all submit button name as "Command".

Then write this below code in your controller page.

Source code:

using MultiButtonMVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MultiButtonMVC.Controllers
{
    public class HomeController : Controller
    {
       
        public ActionResult Load(Login model, string command)
        {
            if (command == "Login")
            {
                // do stuff  
                return RedirectToAction("load");
            }
            else if (command == "Register")
            {
                // do stuff  
                ViewBag.msg = "You have Clicked Register button";
                return View();
            }

            else if (command == "Cancel")
            {
                // do stuff  
                ViewBag.msg = "You have Clicked Cancel Button";
                return View();
            }
            else
            {
                return View();
            }
        }

    }
}

Here "Load" is the View page. We pass 2 arguments in this method. Such as:

public ActionResult Load(Login model, string command)

Here "Login" is the model class and "command" returns the value of all submit buttons.

Post a Comment

0 Comments