第一步:
创建C#,MVC4,基本类型 项目,名称为:CheckBoxDemo
第二步:
在Models目录下创建3个类文件:
Hobby.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CheckBoxDemo.Models
{
public class Hobby
{
public bool CKReading { set; get; }
public bool CKSport { set; get; }
public bool CKMovie { set; get; }
}
}
TextValue.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace CheckBoxDemo.Models
{
public class TextValue
{
public string Text { set; get; }
public bool Value { set; get; }
}
}
MyList.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CheckBoxDemo.Models
{
public class MyList
{
public List<TextValue> MyCheckList
{
get
{
List<TextValue> items = new List<TextValue>();
items.Add(new TextValue { Text = "CKReading", Value =true });
items.Add(new TextValue { Text = "CKSport", Value = false });
items.Add(new TextValue { Text = "CKMovie",Value=true });
return items;
}
}
}
}
第三步:
创建Home控制器.
Home控制器代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CheckBoxDemo.Models;
namespace CheckBoxDemo.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Check1()
{
return View();
}
public ActionResult CheckPost(bool CKReading,bool CKSport,bool CKMovie)
{
ViewBag.CKReading = CKReading;
ViewBag.CKSport = CKSport;
ViewBag.CKMovie = CKMovie;
return View();
}
public ActionResult Check2()
{
return View();
}
public ActionResult CheckPost2(Hobby box)
{
return View(box);
}
public ActionResult Check3()
{
return View();
}
public ActionResult CheckPost3(Hobby box)
{
return View("CheckPost2",box);
}
public ActionResult Check4()
{
return View();
}
public ActionResult CheckPost4(Hobby box)
{
return View(box);
}
public ActionResult CheckList()
{
var v = new MyList();
return View(v);
}
public ActionResult CheckListPost(Hobby box)
{
return View("CheckPost4",box);
}
}
}
第四步:
创建首页视图:Index.cshtml
@{
ViewBag.Title = "Index";
}
<h2>@Html.ActionLink("1.弱视图及弱CheckBox助记符Demo", "Check1", "Home")</h2>
<h2>@Html.ActionLink("2.强视图及强EditorFor助记符、强DisplayFor助记符Demo", "Check2", "Home")</h2>
<h2>@Html.ActionLink("3.强视图及强CheckBoxFor助记符、强DisplayFor助记符Demo-A", "Check3", "Home")</h2>
<h2>@Html.ActionLink("4.强视图及强CheckBoxFor助记符Demo-B", "Check4", "Home")</h2>
<h2>@Html.ActionLink("5.强List视图及弱CheckBox助记符Demo", "CheckList", "Home")</h2>
创建视图:Check1.cshtml:
@{
ViewBag.Title = "Check1";
}
<h2>1.弱视图及弱CheckBox助记符Demo</h2>
@using(Html.BeginForm("CheckPost","Home"))
{
<h2>@Html.CheckBox("CKReading") 读书</h2>
<h2>@Html.CheckBox("CKSport") 体育</h2>
<h2>@Html.CheckBox("CKMovie") 电影</h2>
<input type="submit" value="提交"/>
}
创建视图:Check2.cshtml:
@model CheckBoxDemo.Models.Hobby
@{
ViewBag.Title = "Check2";
}
<h2>2.强视图及强EditorFor、强DisplayFor助记符Demo</h2>
@using(Html.BeginForm("CheckPost2","Home")){
@Html.ValidationSummary(true)
<fieldset>
<legend>Hobby</legend>
<div class="editor-label">
@Html.LabelFor(model => model.CKReading)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CKReading)
@Html.ValidationMessageFor(model => model.CKReading)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CKSport)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CKSport)
@Html.ValidationMessageFor(model => model.CKSport)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CKMovie)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.CKMovie)
@Html.ValidationMessageFor(model => model.CKMovie)
</div>
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "Index")
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
创建视图:Check3.cshtml:
@model CheckBoxDemo.Models.Hobby
@{
ViewBag.Title = "Check3";
}
<h2>3.强视图及强CheckBoxFor助记符、强DisplayFor助记符Demo-A</h2>
@using(Html.BeginForm("CheckPost3","Home")){
<div class="editor-label">
@Html.LabelFor(model => model.CKReading)
</div>
<div class="editor-field">
@Html.CheckBoxFor(model => model.CKReading)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CKSport)
</div>
<div class="editor-field">
@Html.CheckBoxFor(model => model.CKSport)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CKMovie)
</div>
<div class="editor-field">
@Html.CheckBoxFor(model => model.CKMovie)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
创建视图:Check4.csthml:
@model CheckBoxDemo.Models.Hobby
@{
ViewBag.Title = "Check4";
}
<h2>4.强视图及强CheckBoxFor助记符Demo-B</h2>
@using(Html.BeginForm("CheckPost4","Home")){
<div class="editor-label">
@Html.LabelFor(model => model.CKReading)
</div>
<div class="editor-field">
@Html.CheckBoxFor(model => model.CKReading)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CKSport)
</div>
<div class="editor-field">
@Html.CheckBoxFor(model => model.CKSport)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.CKMovie)
</div>
<div class="editor-field">
@Html.CheckBoxFor(model => model.CKMovie)
</div>
<p>
<input type="submit" value="Create" />
</p>
}
创建CheckList.cshtml:
@model CheckBoxDemo.Models.MyList
@{
ViewBag.Title = "CheckList";
}
<h2>
CheckList</h2>
@using (Html.BeginForm("CheckListPost", "Home"))
{
foreach (var item in Model.MyCheckList)
{
var s =item.Text;
@Html.Label(s,s)
@Html.CheckBox(item.Text,item.Value, new{value =item.Value})
}
<hr />
<input type="submit"/>
}
创建CheckPost.cshtml:
@{
ViewBag.Title = "CheckPost";
}
<h2>读书:@ViewBag.CKReading</h2>
<h2>体育:@ViewBag.CKSport</h2>
<h2>电影:@ViewBag.CKMovie</h2>
创建CheckPost2.cshtml:
@model CheckBoxDemo.Models.Hobby
@{
ViewBag.Title = "CheckPost2";
}
<h2>CheckPost2</h2>
<fieldset>
<legend>Hobby</legend>
<div class="display-label">
@Html.DisplayNameFor(model => model.CKReading)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.CKReading)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.CKSport)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.CKSport)
</div>
<div class="display-label">
@Html.DisplayNameFor(model => model.CKMovie)
</div>
<div class="display-field">
@Html.DisplayFor(model => model.CKMovie)
</div>
</fieldset>
<p>
@Html.ActionLink("Edit", "Edit", new { /* id=Model.PrimaryKey */ }) |
@Html.ActionLink("Back to List", "Index")
</p>
创建视图:CheckPost4.cshtml:
@model CheckBoxDemo.Models.Hobby
@{
ViewBag.Title = "CheckPost4";
}
<h2>CheckPost4</h2>
<h2>读书:@Model.CKReading</h2>
<h2>体育:@Model.CKSport</h2>
<h2>电影:@Model.CKMovie</h2>