mvc中HtmlHelper可以帮助我们生成许多Html控件,但是没有类似DropDownList的RadioList,但是发现这些方法都是拓展方法,于是就想自己也拓展一个RadioList
从网上下载mvc源码中发现微软的方法还是很复杂的,添加了模型的校验
下面看一下我的吧
拓展方法
public static class CheeseBarExtend
{
public static MvcHtmlString RadioList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList)
{
return RadioList(htmlHelper, name, selectList, null, null, null, 1);
}
public static MvcHtmlString RadioList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, int col)
{
return RadioList(htmlHelper, name, selectList, null, null, null, col);
}
public static MvcHtmlString RadioList(this HtmlHelper htmlHelper, string name, IEnumerable<SelectListItem> selectList, string rowClass, string radioClass, string spanClass, int col)
{
return RadioListHelper(htmlHelper, metadata: null, name: name, selectList: selectList, rowClass: rowClass, radioClass: radioClass, spanClass: spanClass, col: col);
}
public static MvcHtmlString RadioListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList)
{
return RadioListFor(htmlHelper, expression, selectList, null, null, null, 1);
}
public static MvcHtmlString RadioListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, int col)
{
return RadioListFor(htmlHelper, expression, selectList, null, null, null, col);
}
public static MvcHtmlString RadioListFor<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, IEnumerable<SelectListItem> selectList, string rowClass, string radioClass, string spanClass, int col)
{
if (expression == null)
{
throw new ArgumentNullException("expression");
}
//用来模型验证
ModelMetadata metadata = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
return RadioListHelper(htmlHelper, metadata, ExpressionHelper.GetExpressionText(expression), selectList, rowClass, radioClass, spanClass, col);
}
public static MvcHtmlString RadioListHelper(HtmlHelper htmlHelper, ModelMetadata metadata, string name, IEnumerable<SelectListItem> selectList, string rowClass, string radioClass, string spanClass, int col)
{
StringBuilder resultString = new StringBuilder();
if (radioClass == null) radioClass = "";
if (rowClass == null) rowClass = "";
if (spanClass == null) spanClass = "";
StringBuilder radio = new StringBuilder("<input type=\"radio\" class=\"" + radioClass + "\" name=\"" + name + "\" value=\"noValue\" isChecked /><span class=\"" + spanClass + "\">noText</span>");
StringBuilder tempRadio = new StringBuilder();
StringBuilder tempLine = new StringBuilder();
int tempCol = col;
foreach (SelectListItem selectItem in selectList)
{
tempRadio = new StringBuilder(radio.ToString());
if (selectItem.Selected)
{
tempRadio.Replace("isChecked", "checked");
}
else
{
tempRadio.Replace("isChecked", "");
}
tempRadio.Replace("noValue", selectItem.Value);
tempRadio.Replace("noText", selectItem.Text);
tempLine.Append(tempRadio);
if (--tempCol == 0)
{//要换行
tempLine.WearDiv(rowClass);
resultString.Append(tempLine);
tempCol = col;
tempLine.Clear();
}
}
return new MvcHtmlString(resultString.ToString());
}
public static StringBuilder WearDiv(this StringBuilder sb, string className)
{
string divBeg = "<div class=\"" + className + "\">";
string divEnd = "</div>";
sb.Insert(0, divBeg);
sb.Append(divEnd);
return sb;
}
}
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<object>();
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<object>();
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);
ViewData["RadioSexList"] = sexSelectList;
ViewData["RadioDiplomaList"] = dipSelectList;
return View(person);
}
cshtml
@model MyExtend.Controllers.Person
@{
Layout = null;
ViewBag.Title = "EditPerson";
}
<h2>EditPerson</h2>
@using (Html.BeginForm("SaveEdit", "CheeseBar", FormMethod.Post))
{
<div>
@Html.EditorFor(model => model.Name)
</div>
<div>
@Html.RadioListFor(model => model.Sex, (SelectList)ViewData["RadioSexList"],"rowClass","radioClass","spanClass",1)
</div>
<div>
@Html.RadioList("Diploma", (SelectList)ViewData["RadioDiplomaList"],"rowClass", "radioClass", "spanClass", 2)
</div>
}
编辑后提交,HttpPost入口处添加断点