带有枚举的MVC3 RadioButtonFor

我遇到了HtmlHelper,RadioButtonFor和我模型中的枚举问题。 我有一个强类型视图,我希望复选框切换我的枚举属性。

Enum.cs public enum Values{ Value1, Value2 } 

 Model.cs public class Model{ public Values MyProp{ get; set; } ; 

 View.cshtml @Html.RadioButtonFor(model => model.MyPropi, Values.Values1) 

 Controller.cs public ActionResult WizardFirstStep() { var model = new Model(); return View(model); } 

如果我在控制器中设置MyProperty值,则按预期检查RadioButton。 但是在发送到下一个向导步骤后,将模型作为参数,该属性未设置。

如果它能帮助你理解我的意思:如果它是C#和WPF我将使用IValueConverter。

顺便说一句:我使用HtmlHelper.ActionLink将模型送到控制器。

提前致谢

尝试这个,它应该工作,因为我之前做过同样的事情:

 @Html.RadioButtonFor(model => model.MyProp, (int)Values.Values1, model.MyProp == Values.Values1) 

注意转换为int ,它确保正确的值用于html。

编辑 :对不起,我认为您还需要第三个参数,以确保加载视图时设置正确的单选按钮。

我还假设MyPropi是一个拼写错误并将其更改为MyProp ,请确保在您的最终匹配正确

任何不便敬请谅解。 在这里发帖后,我很快找到了解决方案。 我的ActionLink没有提交@ Html.BeginForm表单。 所以我将我的radiobutton改为:

 @Html.RadioButtonFor(model => model.MyPropi, Values.Values1, new{ onClick = "this.form.submit();" }) 

它将正确的值提交给我的控制器。 目前这没关系。 也许ActionLink可以回发表单数据。

对于Aspx页面:<%:Html.RadioButtonFor(m => m.YourProp,您的枚举的选定值,如:demo1.enum1.value2)