Introduction
This article describes how to create a Custom Modal Popup Box in ASP.NET Web API.
Procedure for creating the Custom Modal Popup Box in the Web API.
Step 1
First create a Web API Application:
-
Start Visual Studio 2012.
-
From the start window select "New Project".
-
In the Template Window select "Installed" -> "Visual C#" -> "Web".
-
Select "ASP.NET MVC 4 Web Application" and click on "OK".

From the "MVC4 Project" window select "Web API".

Step 2
Create a Modal class "Record.cs":
-
In the "Solution Explorer".
-
Right-click on the "Modal" -> "Add" -> "Class".
-
Select "Installed" -> "Visual C#".

Select "Class" and click the "OK" button.
Write the Following code:
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Web;
- namespace CustomModel.Models
- {
- public class Record
- {
- public int ID { get; set; }
- public string Name { get; set; }
- public string Description { get; set; }
- }
- public class RecordManager
- {
- public Collection<Record> Records
- {
- get
- {
- if (HttpRuntime.Cache["Records"] == null)
- this.DisplayData();
- return (Collection<Record>)HttpRuntime.Cache["Records"];
- }
- }
- private void DisplayData()
- {
- var records = new Collection<Record>();
- records.Add(new Record
- {
- ID = 1,
- Name = "Set schedule for saturday",
- Description = "Don't forget to upload this schedule.."
- });
- HttpRuntime.Cache["Records"] = records;
- }
- public Collection<Record> GetAll()
- {
- return Records;
- }
- public Record GetById(int Id)
- {
- return Records.Where(i => i.ID == Id).FirstOrDefault();
- }
- public int Collect(Record detail)
- {
- if (detail.ID <= 0)
- return collectAsNew(detail);
- var availableR = Records.Where(a => a.ID == detail.ID).FirstOrDefault();
- availableR.Name = detail.Name;
- availableR.Description = detail.Description;
- return availableR.ID;
- }
- private int collectAsNew(Record item)
- {
- item.ID = Records.Count + 1;
- Records.Add(item);
- return item.ID;
- }
- }
- }
Step 3
In the "HomeController" file write some code. This file exists in:
-
In the "Solution Explorer".
-
Select "Controller" -> "HomeController".
Add the following code:
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.Mvc;
- using CustomModel.Models;
- namespace CustomModel.Controllers
- {
- public class HomeController : Controller
- {
- public ActionResult Index()
- {
- return View();
- }
- public ActionResult List()
- {
- var mgr = new RecordManager();
- var mode = mgr.GetAll();
- return PartialView(mode);
- }
- public ActionResult Develop()
- {
- var model = new Record();
- return PartialView("RecordForm", model);
- }
- [HttpPost]
- public ActionResult Collect(Record record)
- {
- var mgr = new RecordManager();
- mgr.Collect(record);
- var mode = mgr.GetAll();
- return PartialView("List", mode);
- }
- }
- }









Join the conversation! Your thoughts help the community grow.