2019-5-15 Asp.Net mvc4 CheckBox 控件快速测试Demo

第一步:

创建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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gCodeTop 格码拓普 老师

您的鼓励.我的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值