【发布时间】:2011-11-15 23:52:09
【问题描述】:
对这里的大量代码表示歉意...我已尝试使其尽可能简短:
我已经写了(大部分是从这里偷来的)一个 HtmlHelper 扩展来写出一个基于枚举的 RadioButton(分组)。我还设置了路线图,因此它使用枚举而不是“id”等。
我有两个枚举(对于这个例子)CurrencyType 和 StatusType
当我调用 URL /GBP/Open 时,我的 HtmlHelper 工作正常,并设置了选中 GBP 和 Open 的单选按钮的值。
当我调用 URL /gbp/open 时 - 帮助程序仍然检查值并且似乎可以工作......但是当 RadioButton().ToHtmlString() 被调用时,“已检查”不存在???? 有了默认路由,单选按钮仍然设置正确???
如果你对返回的 SelectList 进行断点,那么你可以看到选择的选项设置正确,所以我有点不知道问题出在哪里?
Empty MVC3 website called Area36...Area 51 was taken ;)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Linq.Expressions;
using System.Text;
namespace Area36.Models
{
public static class Extensions
{
public static SelectList ToSelectList<TEnum>(this TEnum enumObj)
{
var values = from TEnum e in Enum.GetValues(typeof(TEnum))
select new { ID = e, Name = e };
return new SelectList(values, "Id", "Name", enumObj);
}
public static void AppendFormatLine(this StringBuilder sb, string format, params object[] args)
{
sb.AppendFormat(format, args);
sb.AppendLine();
}
public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TProperty>> expression, string fieldSet)
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var e = (TProperty)Enum.Parse(typeof(TProperty), metaData.Model.ToString());
var selectList = e.ToSelectList();
var sb = new StringBuilder();
if (selectList != null)
{
sb.AppendFormatLine("<fieldset><legend>{0}</legend>", fieldSet);
foreach (SelectListItem item in selectList)
{
var id = string.Format("{0}_{1}", metaData.PropertyName, item.Value);
if (htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix != string.Empty)
id.Insert(0, string.Format("{0}_", htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix));
var label = htmlHelper.Label(id, HttpUtility.HtmlEncode(item.Text));
var radio = htmlHelper.RadioButton(name: metaData.PropertyName, value: item.Value, isChecked: item.Selected, htmlAttributes: new { id = id }).ToHtmlString();
sb.AppendFormatLine("<div class=\"radio_{0}\">{1}{2}</div>", metaData.PropertyName, radio, label);
}
sb.AppendLine("</fieldset>");
}
return MvcHtmlString.Create(sb.ToString());
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Area36.Models;
namespace Area36.Controllers
{
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index(CurrencyType currency, StatusType status)
{
return View(new ViewModel { Currency = currency, Status = status });
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Area36.Models
{
public class ViewModel
{
public CurrencyType Currency { get; set; }
public StatusType Status { get; set; }
}
}
@using Area36.Models
@model Area36.Models.ViewModel
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
@Html.RadioButtonForEnum(m => m.Currency, "Currency")
@Html.RadioButtonForEnum(m => m.Status, "Status")
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{currency}/{status}", // URL with parameters
new { controller = "Home", action = "Index", currency = CurrencyType.EUR, status = StatusType.Closed} // Parameter defaults
);
}
【问题讨论】:
标签: c# asp.net-mvc-3 enums radio-button html-helper