RadioButtonFor not selecting value in editor templates

后端 未结 2 2116
抹茶落季
抹茶落季 2020-12-19 13:00

I have the following model:

public class Person
{
    public string Name { get; set; }
    public string Gender { get; set; }
}

I want the

2条回答
  •  青春惊慌失措
    2020-12-19 13:02

    I took this idea and expanded on it. I pass a List> to my editor template as added ViewData that I use to create the radio buttons. My template looks like this

    @model string
    @{
        var buttons = (List>)ViewData["Buttons"];
    }
    @foreach (var button in buttons) {
        
    }
    

    this is what I'm passing to my EditorFor as additional ViewData

    new { Buttons = new List> { new Dictionary { { "Label", "Commercial" }, { "Value", "Y" } }, new Dictionary { { "Label", "Residential" }, { "Value", "N" } } } }
    

    Granted I could add this to a ViewModel type and pass that to the View from my controller, however it was quicker to do it in the cshtml file.

提交回复
热议问题