将枚举传递给 MVC3 的 html.radiobutton
我有一个名为 ActionStatus 的枚举,它有 2 个可能的值 open=0 和 close=1
public enum ActionStatus
{
Open,
Closed
}
我想在编辑中构建单选按钮组并创建使用枚举来填充单选按钮的视图 a) 创建视图中的默认值b) 编辑视图中当前选择的选项。
我是否需要为此提供一个扩展方法,有人已经创建了一个吗?
编辑:在达林斯的回答之后,这是我的模型类
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);
}
枚举在视图中显示正常,但当前选择的选项不会保留到数据库中
I Have an Enum Called ActionStatus that has 2 possible values open=0 and closed = 1
public enum ActionStatus
{
Open,
Closed
}
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?
EDIT: 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
}
and this is my view
@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>
And this is my create controller action
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以强制执行 此辅助方法的枚举类型的通用约束。
然后:
模型:
控制器:
视图:
You could also enforce a generic constraint to an enum type for this helper method.
and then:
Model:
Controller:
View:
只需添加单选按钮
和模型的标签:
Just add labels for radiobuttons
and the Model: