我对HtmlHelper、RadioButtonFor和我的模型中的一个枚举都有问题。我有一个强类型的视图,我希望复选框切换我的enum属性。
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将模型拿到控制器上。
提前感谢
发布于 2012-12-13 16:26:55
给您带来不便,深表歉意。在这里发布之后,我很快就找到了解决方案。我的ActionLink没有提交@Html.BeginForm表单。所以我把我的无线电按钮改为:
@Html.RadioButtonFor(model => model.MyPropi, Values.Values1, new{ onClick = "this.form.submit();" })它向我的控制器提交正确的值。暂时没问题。也许ActionLink可以回发表单数据。
发布于 2012-12-13 16:11:19
尝试一下,它应该能像我以前做过的那样起作用:
@Html.RadioButtonFor(model => model.MyProp, (int)Values.Values1, model.MyProp == Values.Values1)注意对int的强制转换,它确保为html使用正确的值。
编辑:对不起,我认为您还需要第三个参数来确保在加载视图时设置正确的单选按钮。
我还假设MyPropi是一个错误,并将其更改为MyProp,请确保在您的末尾匹配正确。
发布于 2015-09-09 06:40:59
对于Aspx页面:<%:Html.RadioButtonFor(m => m.YourProp,选定的枚举值为:demo1.enum1.value 2)
https://stackoverflow.com/questions/13863790
复制相似问题