asp.net mvc radio button list

前端 未结 1 2030
再見小時候
再見小時候 2021-02-20 16:53

I am trying to display a list of radio buttons for a list of values in ASP.NET MVC. I am confused on how to write the model for the radio button list and the view to be displaye

相关标签:
1条回答
  • 2021-02-20 17:35

    i'm implementing values on viewModel you can take it from model if you want

    viewModel

        public class PropMgmtViewModel
    {
        public Property Property { get; set; }
        public IEnumerable<Property> Properties { get; set; }
        public SelectList Cities { get; private set; }
    
    
        static Dictionary<int, string> CitiesDict = new Dictionary<int, string>()
    {
        { 1 ,"Chicago"},
        { 2 ,"New York"},
        { 3 ,"Zimbabwe"},
    };
        public PropMgmtViewModel()
        {
            Cities = new SelectList(CitiesDict, "Key", "Value");
        }
    

    view code i also included the seleclist propert to show you how it's done

     @foreach (var radioitem in Model.Cities)
        {
            @radioitem.Text;
        @Html.RadioButtonFor(model => model.Property.City, radioitem.Value);
        }
            @Html.DropDownListFor(model => model.Property.City, Model.Cities,"Seciniz") 
    
    0 讨论(0)
提交回复
热议问题