How to use DataViews in MVC to handle the data processing work(CSASPNETMVCDataView)

Introduction

Running the Sample

Please follow the steps below.

Step 1: Open the CSASPNETMVCDataView.sln file. Press "Ctrl+F5" to view the index page.

Step 2: Click on the "Open Person List" link, we will see the index page of Person folder.

Click on the "Create New" link to create a new item.


Click the button to create.

We can click on the "Edit" link to edit the selected item.

We can click on the "Details" to view the item detail.

We can click on the "Delete" to delete the item.

Step 3: Validation is completed.

Using the Code

Code Logical:

Step1. Create a C# ASP.NET MVC 4 Web Application in Visual Studio 2012 and name it as CSASPNETMVCDataView. In the project template tab, select Internet Application and then select ASPX as view engine.

Step2. Create PersonalController.cs file in Controllers folder. The code of the PersonalController file will be shown as below:

C#
Edit|Remove
public class PersonController : Controller
   {
       static List<Person> people = new List<Person>();


       //
       // GET: /Person/


       public ActionResult Index()
       {
           return View(people);
       }


       //
       // GET: /Person/Details/5


       public ActionResult Details(Person p)
       {
           return View(p);
       }


       //
       // GET: /Person/Create


       public ActionResult Create()
       {
           return View();
       } 


       //
       // POST: /Person/Create


       [HttpPost]
       public ActionResult Create(Person p)
       {                      
           if (!ModelState.IsValid)
           {
               return View("Create", p);
           }


           people.Add(p);


           return RedirectToAction("Index");
       }


       //
       // GET: /Person/Delete/5


       public ActionResult Delete(int id)
       {
           Person p = new Person();
           foreach (Person pn in people)
           {
               if (pn.Id == id)
               {
                   p.Name = pn.Name;
                   p.Age = pn.Age;
                   p.Id = pn.Id;
                   p.Phone = pn.Phone;
                   p.Email = pn.Email;
               }
           }


           return View(p);
       }


       //
       // POST: /Person/Delete/5


       [HttpPost]
       public ActionResult Delete(Person p)
       {
           foreach (Person pn in people)
           {
               if (pn.Id == p.Id)
               {
                   people.Remove(pn);
                   break;
               }
           }


           return RedirectToAction("Index");
       }


       //
       // GET: /Person/Edit/5


       public ActionResult Edit(int id)
       {
           Person p = new Person();
           foreach (Person pn in people)
           {
               if (pn.Id == id)
               {
                   p.Name = pn.Name;
                   p.Age = pn.Age;
                   p.Id = pn.Id;
                   p.Phone = pn.Phone;
                   p.Email = pn.Email;
               }
           }


           return View(p);
       }


       //
       // POST: /Person/Edit/5


       [HttpPost]
       public ActionResult Edit(Person p)
       {
           if (!ModelState.IsValid)
           {
               return View("Edit", p);
           }


           foreach (Person pn in people)
           {
               if (pn.Id == p.Id)
               {
                   pn.Name = p.Name;
                   pn.Age = p.Age;
                   pn.Id = p.Id;
                   pn.Phone = p.Phone;
                   pn.Email = p.Email;
               }
           }


           return RedirectToAction("Index");
       }
   }

 


Step3. Create Person.cs file in Models folder. 
The code of the Person file will be shown as below:

C#
Edit|Remove
public class Person
   {
       [Required(ErrorMessage = "The ID is required.")]
       public int Id { get; set; }


       [Required(ErrorMessage = "The name is required.")]
       public string Name { get; set; }


       [Range(1, 200, ErrorMessage = "A number between 1 and 200.")]
       public int Age { get; set; }


       [RegularExpression(@"(^189\d{8}$)|(^13\d{9}$)|(^15\d{9}$)", 
           ErrorMessage = "Invalid phone number. 11 length and start with 13,15,189 prefix. e.g. 13800138000")]
       public string Phone { get; set; }


       [RegularExpression(@"^[\w-\.]+@([\w-]+\.)+[\w-]{2,4}$", 
           ErrorMessage = "Invalid email address.")]
       public string Email { get; set; }
   }

 

 Step4. Add Person folder in Views folder and create the corresponding files. We will add 5 ASPX pages: Create.aspx, Delete.aspx, Details.aspx, Edit.aspx and Index.aspx. Create the logics for Views according to the sample project.
Index.aspx

HTML
Edit|Remove
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">


    

Index

<%foreach (var item in Model) { %> <%} %>
Id Name Age Phone Email
<%= Html.ActionLink("Edit", "Edit", new { id=item.Id }) %> | <%= Html.ActionLink("Details", "Details", item )%> | <%= Html.ActionLink("Delete", "Delete", new { id=item.Id })%> <%= Html.Encode(item.Id) %> <%= Html.Encode(item.Name) %> <%= Html.Encode(item.Age) %> <%= Html.Encode(item.Phone) %> <%= Html.Encode(item.Email) %>

<%= Html.ActionLink("Create New", "Create") %>

</asp:Content>
 

Create.aspx

HTML
Edit|Remove
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">


    

Create

<%using (Html.BeginForm()) {%> <fieldset> <legend>Fields</legend>
<%= Html.LabelFor(model => model.Id) %>
<%= Html.TextBoxFor(model => model.Id )%> <%= Html.ValidationMessageFor(model => model.Id) %>
<%= Html.LabelFor(model => model.Name) %>
<%= Html.TextBoxFor(model => model.Name) %> <%= Html.ValidationMessageFor(model => model.Name) %>
<%= Html.LabelFor(model => model.Age) %>
<%= Html.TextBoxFor(model => model.Age) %> <%= Html.ValidationMessageFor(model => model.Age) %>
<%= Html.LabelFor(model => model.Phone) %>
<%= Html.TextBoxFor(model => model.Phone)%> <%= Html.ValidationMessageFor(model => model.Phone)%>
<%= Html.LabelFor(model => model.Email) %>
<%= Html.TextBoxFor(model => model.Email)%> <%= Html.ValidationMessageFor(model => model.Email)%>

<input type="submit" value="Create" />

</fieldset> <%} %>
<%= Html.ActionLink("Back to List", "Index") %>
</asp:Content>
 

Step5.  Create the logics for models and controllers according to the sample project.

[NOTE] For views pages, the mapping model register info will be added automatically.

For more details of how to process data in MVC2.0, see:http://www.asp.net/mvc/tutorials/creating-model-classes-with-the-entity-framework-cs

Step6: Add other codes to corresponding files according to the sample.

Step7: Now, you can run the page to see the achievement we did before :)

More Information

MVC Tutorials
http://www.asp.net/mvc/tutorials