MVC3 RadioButtonFor与枚举 [英] MVC3 RadioButtonFor with enum

查看:175
本文介绍了MVC3 RadioButtonFor与枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题的HtmlHelper的,RadioButtonFor并在我的模型枚举。我有一个强类型的观点,我想复选框来切换我的枚举属性。

I'm having problems with the HtmlHelper, RadioButtonFor and an enum in my model. I have a strongly typed view, and I want the checkboxes to toggle my enum property.

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的,该单选按钮被选中预期。但后到向导的下一步,它得到了模型参数后,该属性未设置​​。

If I set the MyProperty value in the controller, the RadioButton is checked as expected. But after a post to the next wizard step, which gets the model as parameter, the property isn't set.

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

If it will help you to understand what I mean: If it would be C# and WPF I would use a IValueConverter.

BTW:我用一个HtmlHelper.ActionLink来获取模式控制器

btw: I use a HtmlHelper.ActionLink to get the model to the controller.

在此先感谢

推荐答案

抱歉的任何不便。张贴在这里,我找到了解决办法非常快。我ActionLink的未提交@ Html.BeginForm形式。所以我改变了我的单选按钮为:

Sorry for any inconvenience. After posting here, I found the solution very quickly. My ActionLink was not submitting the @Html.BeginForm form. So i changed my radiobutton to:

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

它提交正确的值到我的控制器。就目前而言,这是好的。也许ActionLink的可以回发的表单数据。

which submits the correct value to my controller. For the moment this is okay. Maybe the ActionLink can post back the form data.

这篇关于MVC3 RadioButtonFor与枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆