Dropdown in MVC

后端 未结 6 674
甜味超标
甜味超标 2020-12-06 15:22

In my MVC application, I need to add a dropdown that would show a list of domain names.

I already have a ViewModel that contains multiple properties. I am not sure w

6条回答
  •  有刺的猬
    2020-12-06 15:45

    In your ViewModel

    public class ViewModelClass
    {
        public string DomainName { get; set; }
    
        public IEnumerable DomainList
        {
            get
            {
                return GetDomainNames()  //your method to get domain names
                 .Select(d => new SelectListItem() { Text = d, Value = d });
            }
        }
    }
    

    your strongly typed view

    @model ViewModelClass
    
    @Html.DropDownListFor(model => model.DomainName, Model.DomainList)
    

提交回复
热议问题