How to create Select List for Country and States/province in MVC

后端 未结 7 871
一生所求
一生所求 2020-12-05 10:19

Hi I am new to MVC and even asp..

I want to create a form in MVC. With the help of some examples I am able to create TextBoxes, but I now I don\'t understand how to

7条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 10:54

    Designing You Model:

    Public class ModelName
    {
        ...// Properties
        public IEnumerable ListName { get; set; }
    }
    

    Prepare and bind List to Model in Controller :

        public ActionResult Index(ModelName model)
        {
            var items = // Your List of data
            model.ListName = items.Select(x=> new SelectListItem() {
                        Text = x.prop,
                        Value = x.prop2
                   });
        }
    

    In You View :

    @Html.DropDownListFor(m => Model.prop2,Model.ListName)
    

提交回复
热议问题