html checkboxlist写法 客户端html,HtmlHelper拓展实现CheckBoxList

经过一番折腾(主要是SelectList这个类操作有些繁琐)实现了CheckBoxList,过程RadioList基本一样

拓展方法

public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable selectList)

{

return CheckBoxList(htmlHelper, name, selectList, null, null, null, 1);

}

public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, int col)

{

return CheckBoxList(htmlHelper, name, selectList, null, null, null, col);

}

public static MvcHtmlString CheckBoxList(this HtmlHelper htmlHelper, string name, IEnumerable selectList, string rowClass, string CheckBoxClass, string spanClass, int col)

{

return CheckBoxListHelper(htmlHelper, metadata: null, name: name, selectList: selectList, rowClass: rowClass, checkBoxClass: CheckBoxClass, spanClass: spanClass, col: col);

}

public static MvcHtmlString CheckBoxListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList)

{

return CheckBoxListFor(htmlHelper, expression, selectList, null, null, null, 1);

}

public static MvcHtmlString CheckBoxListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, int col)

{

return CheckBoxListFor(htmlHelper, expression, selectList, null, null, null, col);

}

public static MvcHtmlString CheckBoxListFor(this HtmlHelper htmlHelper, Expression> expression, IEnumerable selectList, string rowClass, string CheckBoxClass, string spanClass, int col)

{

if (expression == null)

{

throw new ArgumentNullException("expression");

}

//可以不用

ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);

return CheckBoxListHelper(htmlHelper, metadata, ExpressionHelper.GetExpressionText(expression), selectList, rowClass, CheckBoxClass, spanClass, col);

}

public static MvcHtmlString CheckBoxListHelper(this HtmlHelper htmlHelper, ModelMetadata metadata, string name, IEnumerable selectList, string rowClass, string checkBoxClass, string spanClass, int col)

{

StringBuilder resultString = new StringBuilder();

if (checkBoxClass == null) checkBoxClass = "";

if (rowClass == null) rowClass = "";

if (spanClass == null) spanClass = "";

StringBuilder checkBox = new StringBuilder("noText");

StringBuilder tempcheckBox = new StringBuilder();

StringBuilder tempLine = new StringBuilder();

var selectValues = (IEnumerable)((SelectList)selectList).SelectedValue;

int tempCol = col;

foreach (SelectListItem selectItem in selectList)

{

tempcheckBox = new StringBuilder(checkBox.ToString());

if (selectValues.Contains(selectItem.Value))

{

tempcheckBox.Replace("isChecked", "checked");

}

else

{

tempcheckBox.Replace("isChecked", "");

}

tempcheckBox.Replace("noValue", selectItem.Value);

tempcheckBox.Replace("noText", selectItem.Text);

tempLine.Append(tempcheckBox);

if (--tempCol == 0)

{//要换行

tempLine.WearDiv(rowClass);

resultString.Append(tempLine);

tempCol = col;

tempLine.Clear();

}

}

if (tempLine.Length != 0)

{

tempLine.WearDiv("");

}

resultString.Append(tempLine);

return new MvcHtmlString(resultString.ToString());

}

HttpGet

public ActionResult EditPerson(string id)

{

IService service = new Service();

var person = service.GetPersons().FirstOrDefault(lbItem => lbItem.Id == id);

if (person == null)

throw new NullReferenceException();

//性别列表

var sexList = new List();

sexList.Add(new { Value = "nan", Text = "男" });

sexList.Add(new { Value = "nv", Text = "女" });

var sexSelectList = new SelectList(sexList, "Value", "Text",person.Sex);

//学位列表

var dipList = new List();

dipList.Add(new { Value = "dz", Text = "大专" });

dipList.Add(new { Value = "bs", Text = "博士" });

dipList.Add(new { Value = "yjs", Text = "研究生" });

dipList.Add(new { Value = "gz", Text = "高中" });

var dipSelectList = new SelectList(dipList, "Value", "Text",person.Diploma);

//爱好列表

var personHobbies = person.Hobbies.ToList();

var allHobbies = service.GetHobbies().ToList();

var hobbySelectList = new SelectList(allHobbies, "Id", "Name", personHobbies.Select(a => a.Id).ToList());

ViewData["RadioSexList"] = sexSelectList;

ViewData["RadioDiplomaList"] = dipSelectList;

ViewData["CheckBoxHobbyList"] = hobbySelectList;

return View(person);

}

cshtml

@model MyExtend.Controllers.Person

@{

Layout = null;

ViewBag.Title = "EditPerson";

}

EditPerson

@using (Html.BeginForm("SaveEdit", "CheeseBar", FormMethod.Post))

{

@Html.EditorFor(model => model.Name)

@Html.RadioListFor(model => model.Sex, (SelectList)ViewData["RadioSexList"],"rowClass","radioClass","spanClass",1)

@Html.RadioList("Diploma", (SelectList)ViewData["RadioDiplomaList"],"rowClass", "radioClass", "spanClass", 2)

@Html.CheckBoxListFor(model => model.Hobbies, (SelectList)ViewData["CheckBoxHobbyList"],2)

}

3cb51397242e745c314fa50cd0b67cca.png

编辑后提交,save方法添加断点

33d2692f2cc710d94a3cc56c30a776bc.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值