码迷,mamicode.com
首页 > Web开发 > 详细

AspNet MVC4 教学-23:Asp.Net MVC4 Editor 模板技术快速应用Demo

时间:2015-06-03 13:45:45      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:mvc4   asp.net   

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;

namespace MvcEditorTemplatesTest.Models
{
    public class Student
    {
        public int StudentId { get; set; }
        public string Remark { get; set; }
        public Role Role { get; set; }
    }
}

Student.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace MvcEditorTemplatesTest.Models
{
    public class Teacher
    {
        public int TeacherId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public Role Role { get; 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());
        }
    }
}

D.创建View:

在Shared下面创建两个文件:

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 Editor模板", new { style = "width:220px" })

在Home下面创建两个文件:

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 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 Create Teacher","Index")
        </p>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}






AspNet MVC4 教学-23:Asp.Net MVC4 Editor 模板技术快速应用Demo

标签:mvc4   asp.net   

原文地址:http://blog.csdn.net/vinglemar/article/details/46343277

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!