Creating Simple Country Master in MVC 2 Using Without Database

This article dscribes how to create a country form in MVC 2 without a database.

1. Go to Visual Studio 2010 and select "File" -> "New" -> "Project..." then select "Visual C#", select "web" and select "ASP.Net MVC2 Web Application".

2. Open Solution Explorer. It contains default folders like Content, Controllers, Model, Scripts, Views and Global.asaxpage.

Content folder: Contains Style Sheets, Images and so on.
Script Folder: It contains some default jQuery files.
View Folder: Shared (it contains the Master Page).

3. Delete file like (Home and Account) in Model and Controllers.

Step 1: Create a Model in MVC

The first step is to create a Country model. Select the Model folder and right-click then select "Add" -> "New Item". A pop-up will be displayed, select the class and name it countryModel. The Class has six the properties: CountryId, Countryname, Createdby, Createdon,Status and Reason.

For Validation on server-side:

use NameSpace

  1. using System.ComponentModel.DataAnnotations;
Source Code: (CountryModel.cs)

  1. public class Country  
  2. {  
  3.     [Required(ErrorMessage = "This Field is Required")]  
  4.     public int CountryId { getset; }  
  5.     [Required(ErrorMessage = "This Field is Required")]  
  6.     public string CountryName { getset; }  
  7.     [Required(ErrorMessage = "This Field is Required")]  
  8.     public string CreatedBy { getset; }  
  9.     [Required(ErrorMessage = "This Field is Required")]  
  10.     public DateTime CreatedOn { getset; }  
  11.     [Required(ErrorMessage = "This Field is Required")]  
  12.     public bool Status { getset; }  
  13.     [Required(ErrorMessage = "This Field is Required")]  
  14.     public string Reason { getset; }  
  15. } 

Step 2: Define the controller with Action Result

The next step is to select the controller folder and right-click on "Add controller" then it will display a pop-up with the controller name that will be "Deafult Controller" and we can modify the controller name as you like "Country Controler" and check box with (Create , Delete and Update scenario). If you check the check box then it will automatically create the actions methods for (Create, Delete and Update scenario). Add References of the Model  and write the source code as:

Source Code For Controller With Action Methods (CountryController.c):

  1. using Country.Models;  
  2. public class CountryController : Controller  
  3. {  
  4.     static List<CountryModel> cty = new List<CountryModel>();  
  5.     public ActionResult Index()  
  6.     {  
  7.         return View(cty.ToList());  
  8.     }  
  9.     //  
  10.     // GET: /CountryNew/Details/5  
  11.     public ActionResult Details(int id)  
  12.     {  
  13.         var item = cty.Where(x => x.CountryId == id).SingleOrDefault();  
  14.         return View(item);  
  15.     }  
  16.     //  
  17.     // GET: /CountryNew/Create  
  18.     public ActionResult Create()  
  19.     {  
  20.         return View();  
  21.     }  
  22.     //  
  23.     // POST: /CountryNew/Create  
  24.     [HttpPost]  
  25.     public ActionResult Create(CountryModel ctycreate)  
  26.     {  
  27.         try  
  28.         {  
  29.             // TODO: Add insert logic here  
  30.             if (!ModelState.IsValid)  
  31.             {  
  32.                 return View("Create", ctycreate);  
  33.             }  
  34.             cty.Add(ctycreate);  
  35.             return RedirectToAction("Index");  
  36.         }  
  37.         catch  
  38.         {  
  39.             return View();  
  40.         }  
  41.     }  
  42. } 

Step 3: create strongly typed view using the class

  • The next step is to create a view for the Action methods. First select an index action method and right-click on the action method then a pop-up will appear; click on "Add view".
  • The View Name will be displayed and we have two options ("create a partial class view" and "Strong Typed view"). Check on "Strong typed view" then select the "View Data class" Drop down.
  • If data is not displayed in the "view Data Class" then just bulid, then the Project is displayed in the "Model Name Class"(Model Class Name") after selecting the drop down then "View Content"(options like Empty, Edit, Delete, Details, Create and List). Select "List" and click on the "Add" Button then the View is created for the Index Action method.

Continue with the same process for all Action Methods.

Index.aspx(View File)

  1. <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<IEnumerable<CountryInMvc.Models.Country>>" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head id="Head1" runat="server">  
  5.     <title>Index</title>  
  6. </head>  
  7. <body>  
  8.     <table>  
  9.         <tr>  
  10.             <th>  
  11.             </th>  
  12.             <th>CountryId</th>  
  13.             <th>CountryName</th>  
  14.             <th>CreatedBy</th>  
  15.             <th>CreatedOn</th>  
  16.             <th>Status</th>  
  17.             <th>Reason</th>  
  18.         </tr>  
  19.         <% foreach (var item in Model)  
  20.            { %>  
  21.         <tr>  
  22.             <td>  
  23.                 <%: Html.ActionLink("Edit""Edit"new {id=item.CountryId /* id=item.PrimaryKey */ }) %>  
  24.                 |  
  25.                 <%: Html.ActionLink("Details""Details"new {id=item.CountryId /* id=item.PrimaryKey */ })%>  
  26.                 |  
  27.                 <%: Html.ActionLink("Delete""Delete"new {id=item.CountryId/* id=item.PrimaryKey */ })%>  
  28.             </td>  
  29.             <td><%: item.CountryId %></td>  
  30.             <td><%: item.CountryName %></td>  
  31.             <td><%: item.CreatedBy %></td>  
  32.             <td><%:item.CreatedOn %></td>  
  33.             <td><%: item.Status %></td>  
  34.             <td><%: item.Reason %></td>  
  35.         </tr>  
  36.         <% } %>  
  37.     </table>  
  38.     <p>  
  39.         <%: Html.ActionLink("Create New""Create") %>  
  40.     </p>  
  41. </body>  
  42. </html>   

Details.Aspx(Details View)

  1. <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<CountryInMvc.Models.Country>" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head id="Head1" runat="server">  
  5.     <title>Details</title>  
  6. </head>  
  7. <body>  
  8.     <fieldset>  
  9.         <legend>Fields</legend>  
  10.         <div class="display-label">  
  11.             CountryId</div>  
  12.         <div class="display-field">  
  13.             <%: Model.CountryId %></div>  
  14.         <div class="display-label">  
  15.             CountryName</div>  
  16.         <div class="display-field">  
  17.             <%: Model.CountryName %></div>  
  18.         <div class="display-label">  
  19.             CreatedBy</div>  
  20.         <div class="display-field">  
  21.             <%: Model.CreatedBy %></div>  
  22.         <div class="display-label">  
  23.             CreatedOn</div>  
  24.         <div class="display-field">  
  25.             <%:Model.CreatedOn %></div>  
  26.         <div class="display-label">  
  27.             Status</div>  
  28.         <div class="display-field">  
  29.             <%: Model.Status %></div>  
  30.         <div class="display-label">  
  31.             Reason</div>  
  32.         <div class="display-field">  
  33.             <%: Model.Reason %></div>  
  34.     </fieldset>  
  35.     <p>  
  36.         <%: Html.ActionLink("Edit""Edit"new { /* id=Model.PrimaryKey */ }) %>  
  37.         |  
  38.         <%: Html.ActionLink("Back to List""Index") %>  
  39.     </p>  
  40. </body>  
  41. </html>   

Create.aspx(Create View)

 

  1. <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<CountryInMvc.Models.Country>" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html xmlns="http://www.w3.org/1999/xhtml">  
  4. <head id="Head1" runat="server">  
  5.     <title>Create</title>  
  6. </head>  
  7. <body>  
  8.     <% using (Html.BeginForm())  
  9.        {%>  
  10.     <%: Html.ValidationSummary(true) %>  
  11.     <fieldset>  
  12.         <legend>Fields</legend>  
  13.         <div class="editor-label">  
  14.             <%: Html.LabelFor(model => model.CountryId) %>  
  15.         </div>  
  16.         <div class="editor-field">  
  17.             <%: Html.TextBoxFor(model => model.CountryId) %>  
  18.             <%: Html.ValidationMessageFor(model => model.CountryId) %>  
  19.         </div>  
  20.         <div class="editor-label">  
  21.             <%: Html.LabelFor(model => model.CountryName) %>  
  22.         </div>  
  23.         <div class="editor-field">  
  24.             <%: Html.TextBoxFor(model => model.CountryName) %>  
  25.             <%: Html.ValidationMessageFor(model => model.CountryName) %>  
  26.         </div>  
  27.         <div class="editor-label">  
  28.             <%: Html.LabelFor(model => model.CreatedBy) %>  
  29.         </div>  
  30.         <div class="editor-field">  
  31.             <%: Html.TextBoxFor(model => model.CreatedBy) %>  
  32.             <%: Html.ValidationMessageFor(model => model.CreatedBy) %>  
  33.         </div>  
  34.         <div class="editor-label">  
  35.             <%:Html.LabelFor(model=>model.CreatedOn) %>  
  36.         </div>  
  37.         <div class="editor-field, tcalToday">  
  38.             <%:Html.TextBoxFor(model => model.CreatedOn)%>  
  39.             <%:Html.ValidationMessageFor(model => model.CreatedOn )%>  
  40.         </div>  
  41.         <div class="editor-label">  
  42.             <%: Html.LabelFor(model => model.Status) %>  
  43.         </div>  
  44.         <div class="editor-field">  
  45.             <%: Html.TextBoxFor(model => model.Status) %>  
  46.             <%: Html.ValidationMessageFor(model => model.Status) %>  
  47.         </div>  
  48.         <div class="editor-label">  
  49.             <%: Html.LabelFor(model => model.Reason) %>  
  50.         </div>  
  51.         <div class="editor-field">  
  52.             <%: Html.TextBoxFor(model => model.Reason) %>  
  53.             <%: Html.ValidationMessageFor(model => model.Reason) %>  
  54.         </div>  
  55.         <p>  
  56.             <input type="submit" value="Create" />  
  57.         </p>  
  58.     </fieldset>  
  59.     <% } %>  
  60.     <div>  
  61.         <%: Html.ActionLink("Back to List""Index") %>  
  62.     </div>  
  63. </body>  
  64. </html>  

Step 4:

For the next step, open the "Global.asax" file and change the Controller Name to "Country" and change the Action Method to "Index". It is when you request a page from IIS that it goes to the route table and checks the controller and Action Method. In the controller it displays the Index View as the default.


Similar Articles