AspNet MVC4 教学-26:Asp.Net MVC4 原生态Sql技术快速应用Demo
来源:互联网 发布:js热点图 编辑:程序博客网 时间:2024/05/17 04:52
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 Name=@p0", 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.主页启动后,通过学生管理,增加学生。然后,测试[查询]和[删除]功能.
0 0
- AspNet MVC4 教学-26:Asp.Net MVC4 原生态Sql技术快速应用Demo
- AspNet MVC4 教学-19:Asp.Net MVC4 利用Linq技术的搜索应用快速Demo
- AspNet MVC4 教学-20:Asp.Net MVC4 Routing技术快速应用Demo
- AspNet MVC4 教学-22:Asp.Net MVC4 Partial View 技术快速应用Demo
- AspNet MVC4 教学-23:Asp.Net MVC4 Display And Editor 模板技术快速应用Demo
- AspNet MVC4 教学-12:Asp.Net MVC4 Session等技术快速Demo
- AspNet MVC4 教学-13:Asp.Net MVC4 过滤器等技术快速Demo
- AspNet MVC4 教学-14:Asp.Net MVC4 ViewBag等数据传输技术快速比较Demo
- AspNet MVC4 教学-15:Asp.Net MVC4 TempData数据传输技术快速Demo
- AspNet MVC4 教学-21:Asp.Net MVC4 使用Ajax技术获取服务器时间快速Demo
- AspNet MVC4 教学-24:Asp.Net MVC4 Url及Html的Encode技术快速Demo
- AspNet MVC4 教学-25:Asp.Net MVC4 强弱类型View等技术快速对比Demo
- AspNet MVC4 教学-28:Asp.Net MVC4 Ajax技术实现除法取整取余快速Demo
- AspNet MVC4 教学-29:Asp.Net MVC4 jQuery计时器读取服务器时间的Ajax技术快速Demo
- AspNet MVC4 教学-9:Asp.Net MVC4 利用Layout的几种方法的快速Demo
- AspNet MVC4 教学-11:Asp.Net MVC4 默认Authorize及自定义Authorize快速Demo
- AspNet MVC4 教学-16:Asp.Net MVC4 Session及Cookie快速比较Demo
- AspNet MVC4 教学-17:Asp.Net MVC4 全局对象Application快速Demo
- ubuntu14.04下利用源代码安装配置postgresql
- What a superb dog! Golden Retrievers
- 简单易学的机器学习算法——AdaBoost
- csdn添加 微博
- qt 窗口的缩放和滚动条的使用
- AspNet MVC4 教学-26:Asp.Net MVC4 原生态Sql技术快速应用Demo
- 6道SAT阅读填空模拟题
- 学习笔记——XML Schema内置数据类型
- Qt socket。The bound address is already in use
- Android Studio快捷键大全
- vector中针对自定义类型的排序
- TEB PEB
- DH算法
- thinkphp目录结构与系统流程