简体   繁体   English

RadioButtonFor模型绑定不适用于Enum

[英]RadioButtonFor model binding not working with Enum

I am trying to implement a form that contains a grid-like structure of radio buttons. 我正在尝试实现一个包含单选按钮的网格状结构的表单。 There are four radio buttons, one of which can be selected. 有四个单选按钮,可以选择其中一个。 In the model I have an Enum to hold the four options and the model has reference to this. 在模型中,我有一个Enum来容纳四个选项,并且模型对此进行了引用。

The Enum: 枚举:

public enum HubType
{
    Hub5 = 0,
    Hub3 = 1,
    Router = 2,
    Others = 3,
}

The Model: 该模型:

public class Customer
{
    public string ReferenceId { get; set; }
    public CustomerType Type { get; set; }

    public int ChangesMade = 0;
    public DateTime MigrationDate { get; set; }
    public bool MigrationDateChanged = false;
    public List<SiteDetails> Sites = new List<SiteDetails>()
    {
        new SiteDetails { SiteAddress = "test address 1", Hub = HubType.Hub3 },
        new SiteDetails { SiteAddress = "test address 2", Hub = HubType.Hub5},
        new SiteDetails { SiteAddress = "test address 3", Hub = HubType.Router},
        new SiteDetails { SiteAddress = "test address 4", Hub = HubType.Hub5}
    };
}

SiteDetails.cs SiteDetails.cs

public class SiteDetails
{
    public String SiteAddress { get; set; }
    public HubType Hub = HubType.Hub5;
}

Finally the partial view SiteDetails.cshtml 最后局部查看SiteDetails.cshtml

@model MyApp.Models.Customer
@using MyApp.Models.Enums
@{
    var hubTypes = Enum.GetValues(typeof(HubType));
}

@using (Html.BeginForm("SiteDetails", "MyApp", FormMethod.Post))
{
    @for(int site = 0; site < Model.Sites.Count; site++)
    {
        @Model.Sites[site].SiteAddress
        @for (int hub = 0; hub < hubTypes.Length; hub++)
        {
            @Html.RadioButtonFor(m => m.Sites[site].Hub, hubTypes.GetValue(hub))
        }
        <input type="submit" class="button right" value="Save 
    }
}

When I submit the page, I expect the model property to be populated with the selected value, which is not happening now. 提交页面时,我希望模型属性将填充所选的值,但现在不会发生。 Please help me to explore where I am doing wrong and if there is any better approach to do this. 请帮助我探讨我在哪里做错了,以及是否有更好的方法可以做到这一点。 Thanks in advance! 提前致谢!

List<SiteDetails> Sites and HubType Hub are fields, not properties so the DefaultModelBinder cannot set the value. List<SiteDetails> SitesHubType Hub是字段,不是属性,因此DefaultModelBinder无法设置该值。 You need to make them properties 您需要为其设置属性

public class Customer
{
    ....
    public List<SiteDetails> Sites { get; set; }
}
public class SiteDetails
{
    ....
    public HubType Hub = { get; set; }
}

and then assign those values in the controller (or in a parameter-less constructor). 然后在控制器(或无参数构造函数)中分配这些值。 Note also that int ChangesMade and bool MigrationDateChanged are also fields, but you do not appear to be using those in the view. 还要注意, int ChangesMadebool MigrationDateChanged也是字段,但是您似乎没有在视图中使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM