AspNet MVC4 教学-23:Asp.Net MVC4 Display And Editor 模板技术快速应用Demo
来源:互联网 发布:python 数学函数库 编辑:程序博客网 时间:2024/05/17 05:19
A.创建Basic类型的项目.
B.在Model目录下,创建3个文件:
Role.cs:
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace MvcEditorTemplatesTest.Models{ public enum Role { Admin, User, Guest }}
Teacher.cs:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;namespace MvcEditorTemplatesTest.Models{ public class Teacher { public string TeacherId { get { return "8888"; } } public string FirstName { get; set; } public string LastName { get; set; } public Role Role { get { return Role.Guest; } set { ; } } } }
Student.cs:
using System;using System.Collections.Generic;using System.Linq;using System.Web;namespace MvcEditorTemplatesTest.Models{ public class Student { public int StudentId { get { return 88; } set { ; } } public string Remark { get { return "航大学生."; } set { ; } } public Role Role { get { return Role.User; } set { ; } } }}
C.创建HomeControlller.cs:
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.Mvc;using MvcEditorTemplatesTest.Models;namespace MvcEditorTemplatesTest.Controllers{ public class HomeController : Controller { // // GET: /Home/ public ActionResult Index() { return View(new Teacher()); } public ActionResult Student() { return View(new Student()); } public ActionResult StudentDetails() { return View(new Student()); } public ActionResult TeacherDetails() { return View(new Teacher()); } }}
D.创建View:
在Shared下面创建文件夹:EditorTemplates,然后在其中再创建两个文件:
Role.cshtml:
@model MvcEditorTemplatesTest.Models.Role<div id="MyDDL">@Html.DropDownListFor(m => m, new SelectList(Enum.GetNames(Model.GetType()), "Guest"))</div>
String.cshtml:
@model System.String@Html.TextBox("", "比文字来自String编辑模板", new { style = "background:blue;width:220px" })
在Shared下面创建文件夹:DisplayTemplates,然后在其中再创建两个文件:
Role.cshtml:
@model MvcEditorTemplatesTest.Models.Role<div id="MyDDL">@Enum.GetName(Model.GetType(),Model)</div>String.cshtml:
@model System.String@Html.LabelFor(m=>m,Model,new { style = "background:red;width:220px" })在Home下面创建4个文件:
Index.cshtml:
@model MvcEditorTemplatesTest.Models.Teacher@{ ViewBag.Title = "Index";}<h2>Index</h2>@using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Teacher</legend> <div class="editor-label"> @Html.LabelFor(model => model.TeacherId) </div> <div class="editor-field"> @Html.EditorFor(model => model.TeacherId) @Html.ValidationMessageFor(model => model.TeacherId) </div> <div class="editor-label"> @Html.LabelFor(model => model.FirstName) </div> <div class="editor-field"> @Html.EditorFor(model => model.FirstName) @Html.ValidationMessageFor(model => model.FirstName) </div> <div class="editor-label"> @Html.LabelFor(model => model.LastName) </div> <div class="editor-field"> @Html.EditorFor(model => model.LastName) @Html.ValidationMessageFor(model => model.LastName) </div> <div class="editor-label"> @Html.LabelFor(model => model.Role,"以下界面来自Role模板") </div> <div class="editor-field"> @Html.EditorFor(model => model.Role) @Html.ValidationMessageFor(model => model.Role) </div> <p> <input type="submit" value="Create" /> </p> </fieldset>} <p> @Html.ActionLink("Go to get Teacher Details", "TeacherDetails") </p> <p> @Html.ActionLink("Go to Create Student","Student") </p>@section Scripts { @Scripts.Render("~/bundles/jqueryval")}
Student.cshtml:
@model MvcEditorTemplatesTest.Models.Student@{ ViewBag.Title = "Student";}<h2>Student</h2>@using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Student</legend> <div class="editor-label"> @Html.LabelFor(model => model.StudentId) </div> <div class="editor-field"> @Html.EditorFor(model => model.StudentId) @Html.ValidationMessageFor(model => model.StudentId) </div> <div class="editor-label"> @Html.LabelFor(model => model.Remark) </div> <div class="editor-field"> @Html.EditorFor(model => model.Remark) @Html.ValidationMessageFor(model => model.Remark) </div> <div class="editor-label"> @Html.LabelFor(model => model.Role,"以下界面来自Role模板") </div> <div class="editor-field"> @Html.EditorFor(model => model.Role) @Html.ValidationMessageFor(model => model.Role) </div> <p> <input type="submit" value="Create" /> </p> </fieldset>} <p>@Html.ActionLink("Go to get Student Details", "StudentDetails")</p> <p> @Html.ActionLink("Go to Create Teacher","Index") </p>@section Scripts { @Scripts.Render("~/bundles/jqueryval")}StudentDetails.cshtml:
@model MvcEditorTemplatesTest.Models.Student@{ ViewBag.Title = "StudentDetails";}<h2>StudentDetails</h2><fieldset> <legend>Student</legend> <div class="display-label"> @Html.LabelFor(model => model.StudentId) </div> <div class="display-field"> @Html.DisplayTextFor(model => model.StudentId) </div> <div class="display-label"> @Html.DisplayNameFor(model => model.Remark) </div> <div class="display-field"> @Html.DisplayFor(model => model.Remark) </div> <div class="display-label"> @Html.DisplayNameFor(model => model.Role) </div> <div class="display-field"> @Html.DisplayFor(model => model.Role) </div></fieldset>TeacherDetails.cshtml:
@model MvcEditorTemplatesTest.Models.Teacher@{ ViewBag.Title = "TeacherDetails";}<h2>TeacherDetails</h2><fieldset> <legend>Teacher</legend> <div class="display-label"> @Html.DisplayNameFor(model => model.TeacherId) </div> <div class="display-field"> @Html.DisplayFor(model => model.TeacherId) </div> <div class="display-label"> @Html.DisplayNameFor(model => model.FirstName) </div> <div class="display-field"> @Html.DisplayFor(model => model.FirstName) </div> <div class="display-label"> @Html.DisplayNameFor(model => model.LastName) </div> <div class="display-field"> @Html.DisplayFor(model => model.LastName) </div> <div class="display-label"> @Html.DisplayNameFor(model => model.Role) </div> <div class="display-field"> @Html.DisplayFor(model => model.Role) </div></fieldset>
F.在Site.css文件中的末尾增加:
#MyDDL{ width: 150px; height: 26px; background-color: #FF00FF;}
0 0
- AspNet MVC4 教学-23:Asp.Net MVC4 Display And Editor 模板技术快速应用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 教学-26:Asp.Net MVC4 原生态Sql技术快速应用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
- 使用Git上传本地项目代码到github
- ★营销QQ辅助软件如何迅速添加好友★
- 查看程序占用tomcat内存情况
- shell脚本中echo显示内容带颜色 【常用】
- 类与对象的创建详解(适合新人)
- AspNet MVC4 教学-23:Asp.Net MVC4 Display And Editor 模板技术快速应用Demo
- 第7章 第1题
- 旋转90度也可以,Lumia的四大重置方式
- ArcGIS教程:区域填充
- velocity 配置文件详解
- ★营销QQ辅助软件如何迅速添加好友★
- thread 中join() 的用法
- Sqoop-1.4.4工具import和export使用详解
- leetcode Pow(x, n)