您的位置:首页 > 编程语言 > ASP

asp.net mvc checkboxlist的简单实现

2017-02-22 09:17 513 查看
View中:

<h2>Test2_Checkboxlist</h2>
@using MedCrab.Core.Model.APP
@{
List<htTagEx> tags = ViewBag.Tags;
}
<div class="margin:10px auto;">
@{

foreach(var t in tags)   //设置伪Checkboxlist
{
string _checked = "";  //设置_checked变量进行checkbox的默认设置选项
<label>
@if(t.fState==0) //fState为数据库字段,设置保存此标签是否被选择
{
<input type="checkbox" name="tags" value="@t.fName" @_checked/>@t.fName
}
else
{
_checked = "checked=";
<input type="checkbox" name="tags" value="@t.fName" @_checked />@t.fName
}
</label>
}
}

</div>

Controller中:

/// <summary>
/// 显示标签
/// </summary>
/// <returns></returns>
[HttpGet]
public ActionResult Test2()
{
//从数据库中拿出所有标签进行显示
TagService tagService=new TagService();
ViewBag.Tags = tagService.GetTagList(); //得到标签列表
return View();
}
/// <summary>
/// 对已经选择的标签进行识别保存
/// </summary>
/// <returns></returns>
[HttpPost]
public ActionResult Test2(htTag tags)
{
TagService tagService = new TagService();
ViewBag.Tags = tagService.GetTagList();
if (ModelState.IsValid) //是否被用户选中
{
//填充保存选择标签
}
return View();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: