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.主页启动后,通过学生管理,增加学生。然后,测试[查询]和[删除]功能.

时间: 2024-12-10 10:23:30

AspNet MVC4 教学-26:Asp.Net MVC4 原生态Sql技术快速应用Demo的相关文章

AspNet MVC4 教学-25:Asp.Net MVC4 强弱类型View等技术高速对照Demo

A.创建Basic类型项目. B.Model文件夹下创建4个类文件: Teacher.cs: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcViewModelTest.Models { public class Teacher { public string Name { get { return "马老师"; } } public st

AspNet MVC4 教学-14:Asp.Net MVC4 ViewBag

 http://www.duobei.com/people/5656146230/ http://www.duobei.com/people/4564554477/ http://www.duobei.com/people/3508553410/ http://www.duobei.com/people/7114376700/ http://www.duobei.com/people/7422820143/ http://www.duobei.com/people/3186346777/ h

AspNet MVC4 教学-25:Asp.Net MVC4 强弱类型View等技术快速对比Demo

A.创建Basic类型项目. B.Model目录下创建4个类文件: Teacher.cs: using System; using System.Collections.Generic; using System.Linq; using System.Web; namespace MvcViewModelTest.Models { public class Teacher { public string Name { get { return "马老师"; } } public str

AspNet MVC4 教学-16:Asp.Net MVC4 Session及Cookie快速比较Demo

创建basic类型mvc. HomeController.cs: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcSessionCookieTest.Controllers { public class HomeController : Controller { // // GET: /Home/ publ

AspNet MVC4 教学-27:Asp.Net MVC4 自定义helper及function的快速Demo

A.创建Basic类型项目. B.创建App_Code文件夹,在里面创建2个cshtml文件: MyHelper.cshtml: @helper MyTruncate(string input, int length) { <text>来自App_Code中的Helper->MyTruncate:</text> if (input.Length <= length) { @input } else { @input.Substring(0, length) } } @h

AspNet MVC4 教学-9:Asp.Net MVC4 利用Layout的几种方法的快速Demo

HomeController.cs文件内容: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcLayoutTest.Controllers { public class HomeController : Controller { // // GET: /Home/ public ActionResult I

AspNet MVC4 教学-14:Asp.Net MVC4 ViewBag等数据传输技术快速比较Demo

新建一个Basic类型的Project. 1.HomeCtroller.cs: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcViewBagTest.Controllers { public class HomeController : Controller { //GET: /Home/ public

AspNet MVC4 教学-21:Asp.Net MVC4 Ajax技术快速应用Demo

A.首先创建basic类型的项目. B.创建HomeController.cs文件: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; namespace MvcAjaxTest.Controllers { public class HomeController : Controller { // // GET: /Homw/ pub

AspNet MVC4 教学-20:Asp.Net MVC4 Routing技术快速应用Demo

A.创建一个Basic类型的MVC项目. B.在Content文件目录下创建下载文件资源:cs.rar,cs.doc,cs.txt等,见下图右方: C.修改RouteConfig.cs文件: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.Web.Routing; namespace MvcRouteTes