Razor视图使用枚举源捕获RadioButtonFor

时间:2013-03-07 13:39:46

标签: c# asp.net-mvc razor

我正在使用MVC3和Razor视图,我的视图填充了正确的数据

HelperModel

public partial class Visibility
    {
        public enum VisibilityLevel { ShowThis, HideThis, HideAll }
        ...
    }
public partial class Address
    {
        public Visibility.VisibilityLevel Visibility{
            get {
                ...

查看

    @Html.RadioButtonFor(m => m.HIDE_DATA, Models.Visibility.VisibilityLevel.ShowThis ) <span>Show @Model.TYPE_DESC address</span><br />
    @Html.RadioButtonFor(m => m.HIDE_DATA, Models.Visibility.VisibilityLevel.HideThis) <span>Don't show @Model.TYPE_DESC address </span><br />
    @Html.RadioButtonFor(m => m.HIDE_DATA, Models.Visibility.VisibilityLevel.HideAll) <span>Don't show ANY addresses</span><br />

控制器

 //
 // POST: /Address/Edit/5

 [HttpPost]
 public ActionResult Edit(Address addr,string submitButton)
 {
     ...

这会根据数据和模型定义正确填充页面,但是回发没有获得新值。当我检查控制器时,传递给提交的地址模型具有RadioButton的原始值,而不是用户选择或更改的值。我错过了什么?

1 个答案:

答案 0 :(得分:0)

原来不是enum的问题,我的自定义属性没有设置器。

已更改

<强> HelperModel

    Visibility.VisibilityLevel _vizibility;

    public Visibility.VisibilityLevel Visibility{
        get {
            if (_vizibility != Models.Visibility.VisibilityLevel.ShowThis)  //the default
                return _hide_data;
            var ans =  new MacDirectEntities()
            ...
            return ans.Visibility;
        }
        set {
            _vizibility= value;
        }
    }
}