通过枚举到html.radiobuttonfor MVC3 [英] pass enum to html.radiobuttonfor MVC3
问题描述
我有一个Enum被称为ActionStatus,它有2个可能的值open = 0并且closed = 1
I Have an Enum Called ActionStatus that has 2 possible values open=0 and closed = 1
public enum ActionStatus
{
Open,
Closed
}
我想在我的编辑中创建单选按钮组,并创建使用枚举填充单选按钮的视图:a)创建视图中的默认值,以及b)编辑视图中当前选定的选项。
I want to build radio button group in my edit and create views that uses the enum to populate the radio buttons with a) a default value in the create view and b) the currently selected option in the edit view.
我需要一个扩展方法,有没有人已经创建了一个?
Do i need an extension method for this, and has anybody already created one?
编辑:在Darins的答案之后,这是我的Model类
After Darins's answer below this is my Model class
namespace Actioner.Models
{
[MetadataType(typeof(MeetingActionValidation))]
public class MeetingAction
{
[Key]
public int MeetingActionId { get; set; }
[Required]
[Display(Name = "Description")]
public string Description { get; set; }
[Required]
[Display(Name = "Review Date")]
public DateTime ReviewDate { get ;set; }
public int Status{ get; set; }
[ScaffoldColumn(false)]
public int MeetingId { get; set; }
//public virtual Meeting Meeting { get; set; }
//public virtual ICollection<User> Users { get; set; }
public virtual ICollection<ActionUpdate> ActionUpdates { get; set; }
public MeetingActionStatus ActionStatus { get; set; }
}
public enum MeetingActionStatus
{
Open,
Closed
}
这是我的观点
@model Actioner.Models.MeetingAction
@using Actioner.Helpers
<div class="editor-label">
@Html.LabelFor(model => model.Description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Description)
@Html.ValidationMessageFor(model => model.Description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ReviewDate)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReviewDate)
@Html.ValidationMessageFor(model => model.ReviewDate)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Status)
</div>
<div class="editor-field">
@Html.RadioButtonForEnum(x => x.ActionStatus)
</div>
这是我创建控制器操作
public virtual ActionResult Create(int id)
{
var meetingAction = new MeetingAction
{
MeetingId = id,
ReviewDate = DateTime.Now.AddDays(7)
};
ViewBag.Title = "Create Action";
return View(meetingAction);
}
枚举在视图中显示正常,但当前选定的选项不会保留到数据库
The enum displays fine in the view, but the currently selected option is not persisted to the database
推荐答案
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
namespace YourNamespace
{
public static class HtmlExtensions
{
public static MvcHtmlString RadioButtonForEnum<TModel, TProperty>(
this HtmlHelper<TModel> htmlHelper,
Expression<Func<TModel, TProperty>> expression
)
{
var metaData = ModelMetadata.FromLambdaExpression(expression, htmlHelper.ViewData);
var names = Enum.GetNames(metaData.ModelType);
var sb = new StringBuilder();
foreach (var name in names)
{
var id = string.Format(
"{0}_{1}_{2}",
htmlHelper.ViewData.TemplateInfo.HtmlFieldPrefix,
metaData.PropertyName,
name
);
var radio = htmlHelper.RadioButtonFor(expression, name, new { id = id }).ToHtmlString();
sb.AppendFormat(
"<label for=\"{0}\">{1}</label> {2}",
id,
HttpUtility.HtmlEncode(name),
radio
);
}
return MvcHtmlString.Create(sb.ToString());
}
}
}
您还可以强制执行<枚举类型的一般约束href =http://msmvps.com/blogs/jon_skeet/archive/2009/09/10/generic-constraints-for-enums-and-delegates.aspx =noreferrer> 这个帮助方法。
You could also enforce a generic constraint to an enum type for this helper method.
然后:
型号:
public enum ActionStatus
{
Open,
Closed
}
public class MyViewModel
{
public ActionStatus Status { get; set; }
}
控制器:
public class HomeController : Controller
{
public ActionResult Index()
{
return View(new MyViewModel
{
Status = ActionStatus.Closed
});
}
[HttpPost]
public ActionResult Index(MyViewModel model)
{
return View(model);
}
}
查看:
@model MyViewModel
@using (Html.BeginForm())
{
@Html.RadioButtonForEnum(x => x.Status)
<input type="submit" value="OK" />
}
这篇关于通过枚举到html.radiobuttonfor MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!