A.创建Basic类型项目.
B.在Model目录下面创建以下文件:
Student.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.ComponentModel.DataAnnotations.Schema; namespace MvcSqlTest.Models { public class Student { public int Id { set; get; } public string Name { set; get; } public int Age { set; get; } public string Class { set; get; } [NotMapped] public string Country { get { return "China"; } } } }
C.创建Controller:
HomeController.cs:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcSqlTest.Controllers { public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { return View(); } } }
StudentController.cs:
using System; using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.Linq; using System.Web; using System.Web.Mvc; using MvcSqlTest.Models; using System.Transactions; namespace MvcSqlTest.Controllers { public class StudentController : Controller { private MvcSqlTestContext db = new MvcSqlTestContext(); public ActionResult Index() { return View(db.Students.ToList()); } public ActionResult SelectSql(string Name) { Student student = db.Students.SqlQuery("select * from Students where [email protected]", Name).Single(); return View("Details",student); } public ActionResult DeleteSql(string Name) { using (TransactionScope ts = new TransactionScope()) { string sql = "delete from Students where Name={0}"; db.Database.ExecuteSqlCommand(sql, Name); ts.Complete(); } return RedirectToAction("Index"); } public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(Student student) { if (ModelState.IsValid) { db.Students.Add(student); db.SaveChanges(); return RedirectToAction("Index"); } return View(student); } protected override void Dispose(bool disposing) { db.Dispose(); base.Dispose(disposing); } } }
D.创建相应的View:
Home/Index.cshtml:
@{ ViewBag.Title = "Index"; } <h2>Index</h2> <h2>@Html.ActionLink("学生管理","Index","Student")</h2>
提示:通过平台技术以及Entity Framework技术产生Student目录下的View:
Create.cshtml,Details.cshtml,Index.cshtml,同时产生相应的数据库文件.
再一次,修改Index.cshtml:
@model IEnumerable<MvcSqlTest.Models.Student> @{ ViewBag.Title = "Index"; } <h2>Index</h2> @using (Html.BeginForm("SelectSql","Student")) { @Html.TextBox("Name"); <input type="submit" value="查询" /> } @using (Html.BeginForm("DeleteSql","Student")) { @Html.TextBox("Name"); <input type="submit" value="删除" /> } <p> @Html.ActionLink("Create New", "Create") </p> <table> <tr> <th> @Html.DisplayNameFor(model => model.Name) </th> <th> @Html.DisplayNameFor(model => model.Age) </th> <th> @Html.DisplayNameFor(model => model.Class) </th> <th> @Html.DisplayNameFor(model => model.Country) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Name) </td> <td> @Html.DisplayFor(modelItem => item.Age) </td> <td> @Html.DisplayFor(modelItem => item.Class) </td> <td> @Html.DisplayFor(modelItem => item.Country) </td> </tr> } </table>
E.主页启动后,通过学生管理,增加学生。然后,测试[查询]和[删除]功能.
时间: 2024-12-10 10:23:30