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

來源:互聯網
上載者:User

標籤:asp.net   mvc4   

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

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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.