Preface:This example demonstrates how to create an MVC program and does not focus on the Code details. errors are inevitable. Sorry.
Lab objectives:Create an MVC program to operate on database data.
Development Environment:VS2010 (ASP. net mvc 2 ).
Database:SQL Server 2008. Create an instance database TestDB, which only contains the table tb_Student.
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + MSDQwr2ow/5E/VV2Vi06bTw7PM0PKhozwvcD4KPHA + IDxpbWcgc3JjPQ = "http://www.bkjia.com/uploadfile/Collfiles/20131214/20131214090442154.jpg" alt = "\">
2. Add the Entity Data Model of the database table.
2.1 right-click the "Models" folder and choose "add"> "new item" from the shortcut menu to open the "Add new item" dialog box. Select "ADO. NET Object Data Model" and enter the name "TestDB. edmx ". Click Add.
2.2 next, go to the "select model content" screen and select the "generate from database" option. Click "Next.
2.3 next, go to the "select your data connection" screen and set the database connection information. For specific settings, see. After setting the database connection, click "Next.
To set database connection for the first time, click "create connection" to go to the "Connection Properties" setting screen and set database connection information. For more information about settings, see settings.
2.4 next, go to the "select database object" screen and select the table tb_Student. Click "finish" to create the database table Entity Data Model.
3. Add a controller.
Add the Controller HomeController In the Controllers folder. Select "add operation methods for the Create, Update, Delete, and Details schemes ".
Click "add" to generate the HomeController. cs file. The generated HomeController. cs file automatically contains the following code.
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc; namespace MyFirstMvcExp.Controllers{ public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { return View(); } // // GET: /Home/Details/5 public ActionResult Details(int id) { return View(); } // // GET: /Home/Create public ActionResult Create() { return View(); } // // POST: /Home/Create [HttpPost] public ActionResult Create(FormCollection collection) { try { // TODO: Add insert logic here return RedirectToAction("Index"); } catch { return View(); } } // // GET: /Home/Edit/5 public ActionResult Edit(int id) { return View(); } // // POST: /Home/Edit/5 [HttpPost] public ActionResult Edit(int id, FormCollection collection) { try { // TODO: Add update logic here return RedirectToAction("Index"); } catch { return View(); } } // // GET: /Home/Delete/5 public ActionResult Delete(int id) { return View(); } // // POST: /Home/Delete/5 [HttpPost] public ActionResult Delete(int id, FormCollection collection) { try { // TODO: Add delete logic here return RedirectToAction("Index"); } catch { return View(); } } }}
4. Rewrite the Index () method and add a view.
The code for modifying the Index () method is as follows:
public ActionResult Index(){ var model = TestDB.tb_Student.ToList(); return View(model);}
TestDB is defined as follows:
TestDBEntities TestDB = new TestDBEntities();
Before using TestDBEntities, add reference:
using MyFirstMvcExp.Models;
Right-click the Index () method and choose "add view" from the shortcut menu to open the "add view" dialog box.
Set the view name to "Index", check "Create strong type View", and set the View data class to "myfir1_vcexp. models. tb_Student, The View content is set to "List", and the "select template page" option is not checked.
Click "add" to generate the Index. aspx file. The Code is as follows.
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage
>" %>
Index
|
student_id |
student_name |
student_sex |
student_age |
class_id |
teacher_name |
<% foreach (var item in Model) { %>
| <%: Html.ActionLink("Edit", "Edit", new { id=item.student_id }) %> | <%: Html.ActionLink("Details", "Details", new { id=item.student_id })%> | <%: Html.ActionLink("Delete", "Delete", new { id=item.student_id })%> |
<%: item.student_id %> |
<%: item.student_name %> |
<%: item.student_sex %> |
<%: item.student_age %> |
<%: item.class_id %> |
<%: item.teacher_name %> |
<% } %>
<%: Html.ActionLink("Create New", "Create") %>
Run the program. The result is shown in.
At this point, click the "Edit", "Details", "Delete", and "Create New" buttons, the program will fail, because these functions are not implemented.
5. Rewrite the Edit () method and add a view.
The code for modifying the Edit () method is as follows:
//// GET: /Home/Edit/5 [AcceptVerbs(HttpVerbs.Get)]public ActionResult Edit(int id){ var model = TestDB.tb_Student.First(c => c.student_id == id); return View(model);} //// POST: /Home/Edit/5 [AcceptVerbs(HttpVerbs.Post)]public ActionResult Edit(int id, FormCollection from){ try { // TODO: Add update logic here var model = TestDB.tb_Student.First(c => c.student_id == id); UpdateModel(model, new[] { "student_name", "student_sex", "student_age", "class_id", "teacher_name" }); TestDB.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); }}On the "add view" screen, set the view name to "Edit" and set the View content to "Edit". Other settings are the same as those for the Index view.
Click "add" to generate the Edit. aspx file. The Code is as follows.
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage
" %>
Edit <% using (Html.BeginForm()) {%> <%: Html.ValidationSummary(true) %>
Fields <%: Html.LabelFor(model => model.student_id) %> <%: Html.TextBoxFor(model => model.student_id) %> <%: Html.ValidationMessageFor(model => model.student_id) %> <%: Html.LabelFor(model => model.student_name) %> <%: Html.TextBoxFor(model => model.student_name) %> <%: Html.ValidationMessageFor(model => model.student_name) %> <%: Html.LabelFor(model => model.student_sex) %> <%: Html.TextBoxFor(model => model.student_sex) %> <%: Html.ValidationMessageFor(model => model.student_sex) %> <%: Html.LabelFor(model => model.student_age) %> <%: Html.TextBoxFor(model => model.student_age) %> <%: Html.ValidationMessageFor(model => model.student_age) %> <%: Html.LabelFor(model => model.class_id) %> <%: Html.TextBoxFor(model => model.class_id) %> <%: Html.ValidationMessageFor(model => model.class_id) %> <%: Html.LabelFor(model => model.teacher_name) %> <%: Html.TextBoxFor(model => model.teacher_name) %> <%: Html.ValidationMessageFor(model => model.teacher_name) %>
<% } %> <%: Html.ActionLink("Back to List", "Index") %>
Run the program again and click "Edit" to open the following screen.
6. Rewrite the Details () method and add a view.
The code for modifying the Details () method is as follows:
[AcceptVerbs(HttpVerbs.Get)]public ActionResult Details(int id){ var model = TestDB.tb_Student.First(c => c.student_id == id); return View(model);}On the "add view" screen, set the view name to "Details" and set the View content to "Details". Other settings are the same as those of the Index view.
Click "add" to generate the Details. aspx file. The Code is as follows.
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage
" %>
Details
Fields student_id <%: Model.student_id %> student_name <%: Model.student_name %> student_sex <%: Model.student_sex %> student_age <%: Model.student_age %> class_id <%: Model.class_id %> teacher_name <%: Model.teacher_name %>
<%: Html.ActionLink("Edit", "Edit", new { id=Model.student_id }) %> | <%: Html.ActionLink("Back to List", "Index") %>
Run the program again and click "Details" to open the following screen.
7. Rewrite the Create () method and add a view.
The code for modifying the Create () method is as follows:
//// GET: /Home/Create [AcceptVerbs(HttpVerbs.Get)]public ActionResult Create(){ tb_Student student = new tb_Student(); return View(student);} //// POST: /Home/Create [AcceptVerbs(HttpVerbs.Post)]public ActionResult Create(int id, FormCollection form){ try { // TODO: Add insert logic here var model = TestDB.tb_Student.First(c => c.student_id == id); if (model == null) { tb_Student student = new tb_Student(); UpdateModel(student, new[] { "student_name", "student_sex", "student_age", "class_id", "teacher_name" }); TestDB.AddTotb_Student(student); TestDB.SaveChanges(); return RedirectToAction("Index"); } else { return RedirectToAction("Create"); } } catch { return View(); }}On the "add view" screen, set the view name to "Create" and set the View content to "Create". Other settings are the same as those for the Index view.
Click "add" to generate the file Create. aspx. The Code is as follows.
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage
" %>
Create <% using (Html.BeginForm()) {%> <%: Html.ValidationSummary(true) %>
Fields <%: Html.LabelFor(model => model.student_id) %> <%: Html.TextBoxFor(model => model.student_id) %> <%: Html.ValidationMessageFor(model => model.student_id) %> <%: Html.LabelFor(model => model.student_name) %> <%: Html.TextBoxFor(model => model.student_name) %> <%: Html.ValidationMessageFor(model => model.student_name) %> <%: Html.LabelFor(model => model.student_sex) %> <%: Html.TextBoxFor(model => model.student_sex) %> <%: Html.ValidationMessageFor(model => model.student_sex) %> <%: Html.LabelFor(model => model.student_age) %> <%: Html.TextBoxFor(model => model.student_age) %> <%: Html.ValidationMessageFor(model => model.student_age) %> <%: Html.LabelFor(model => model.class_id) %> <%: Html.TextBoxFor(model => model.class_id) %> <%: Html.ValidationMessageFor(model => model.class_id) %> <%: Html.LabelFor(model => model.teacher_name) %> <%: Html.TextBoxFor(model => model.teacher_name) %> <%: Html.ValidationMessageFor(model => model.teacher_name) %>
<% } %> <%: Html.ActionLink("Back to List", "Index") %>
Run the program again and click "Create New" to open the following screen.
5. Rewrite the Delete () method and add a view.
The code for modifying the Delete () method is as follows:
[AcceptVerbs(HttpVerbs.Get)]public ActionResult Delete(int id){ try { var model = TestDB.tb_Student.First(c => c.student_id == id); TestDB.tb_Student.DeleteObject(model); TestDB.SaveChanges(); return RedirectToAction("Index"); } catch { return View(); }}
On the "add view" screen, set the view name to "Delete" and the View content to "Delete". Other settings are the same as those for the Index view.
Click "add" to generate the Delete. aspx file. The Code is as follows.
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage
" %>
Delete Are you sure you want to delete this?
Fields student_id <%: Model.student_id %> student_name <%: Model.student_name %> student_sex <%: Model.student_sex %> student_age <%: Model.student_age %> class_id <%: Model.class_id %> teacher_name <%: Model.teacher_name %>
<% using (Html.BeginForm()) { %>
| <%: Html.ActionLink("Back to List", "Index") %>
<% } %>
Run the program again and click "Delete" in the first record to Delete the first record.