1. 程式人生 > >AspNet MVC4 教學-26:Asp.Net MVC4 原生態Sql技術快速應用Demo

AspNet MVC4 教學-26:Asp.Net MVC4 原生態Sql技術快速應用Demo

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.主頁啟動後,通過學生管理,增加學生。然後,測試[查詢]和[刪除]功能.