Multiple Button Click Event Handling in Web API

Introduction

This article explains how to handle multiple button click event handlings in the ASP.NET Web API. These events are present in the same View.

The following is the procedure for creating the application.

Step 1

First create a Web API application.

  • Start Visual Studio 2013.
  • From the Start Window select "New Project".
  • From the new project window select "Installed" -> "Visual C#" -> "Web" -> "Visual Studio 2012".
  • Select "ASP.NET MVC 4 Application" and click the "Ok" button.

    b.jpg

  • From the "MVC 4 Project" window select "Web API".

    b1.jpg

  • Click on the "OK" buuton.

Step 2

Add a Model Class as in the following:

  • In the "Solution Explorer".
  • Right-click on the "Model folder".
  • Select "Add" -> "class".
  • From  the add item window select "Installed" -> "Visual C#".

    b2.jpg

  • Select "Class" and click on the "Add" button.

Add the following Code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Reflection;  
  5. using System.Web;  
  6. using System.Web.Mvc;  
  7. namespace ButtonEvent.Models  
  8. {  
  9.     public class AttributeModel : ActionMethodSelectorAttribute  
  10.     {  
  11.         public string Name { getset; }  
  12.         public string Data { getset; }  
  13.         public override bool IsValidForRequest(ControllerContext context, MethodInfo Info)  
  14.         {  
  15.             var request = context.RequestContext.HttpContext.Request;  
  16.             return request.Form[this.Name.Trim()] == this.Data;  
  17.         }  
  18.     }  
  19. } 

The "ActionMethoSelectorAttribute" allow the selection of the "action" method. Inherit this abstract class to create the attribute of the "AttributeModel" class.

Step 3

Now in the "Controller" we add the code that uses all the variables of the Model class. This file exists:

  • In the "Solution Explorer".
  • Expand the "Controller" folder.
  • Select the "HomeController".

    b3.jpg

Add the following code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using ButtonEvent.Models;  
  7. namespace ButtonEvent.Controllers  
  8. {  
  9.     public class HomeController : Controller  
  10.     {  
  11.         public ActionResult Index()  
  12.         {  
  13.            return View();  
  14.         }  
  15.         [ActionName("Index")]  
  16.         [AcceptVerbs(HttpVerbs.Post)]  
  17.         [AttributeModel(Name = "Number", Data = "ShowEvent1")]  
  18.         public ActionResult ShowEvent1()  
  19.         {  
  20.             ViewBag.EventName = "This is Button 1 Event";  
  21.             return View();  
  22.         }  
  23.         [ActionName("Index")]  
  24.         [AcceptVerbs(HttpVerbs.Post)]  
  25.         [AttributeModel(Name = "Number", Data = "ShowEvent2")]  
  26.         public ActionResult ShowEvent2()  
  27.         {  
  28.             ViewBag.EventName = "This is Button 2 Event";  
  29.             return View();  
  30.         }  
  31.     }  
  32. }   

"[AcceptVerbs(HttpVerbs.Post)]" This is sane as "[HttpPost]". but the difference is that If we want that action respond both Get and Post Then we can not use [HttpGet,HttpPost] on same action. But we can use [AcceptVerbs(HttpVerb.Get|HttpVerb.Post)].

"[AttributeModel(Name = "Number", Data = "ShowEvent2")]" This indicates, accessing the variable of the AttributeModel class.

Step 4

In the View write some code as in the following:

  • In the "Solution Explorer".

  • Expand the "Views folder".

  • Select "Home" -> "Index.cshtml".

    b4.jpg

Add the following code:

  1. @{  
  2.     ViewBag.Title = "Index";  
  3. }  
  4. @using (Html.BeginForm("Index""Home"))  
  5. {  
  6.     <h2>@ViewBag.EventName</h2>  
  7.     <input type="submit" value="ShowEvent1" name="Number" />  
  8.     <input type="submit" value="ShowEvent2" name="Number" />  
  9. }   

Step 5

Execute the application:

b5.jpg

Click on the "ShowEvent1" button.

b6.jpg

Now click on the "ShowEvent2" button.

b7.jpg


Similar Articles