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

后端 未结 7 870
一生所求
一生所求 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条回答
  •  时光说笑
    2020-12-05 10:57

    Thank You All! I am able to to load Select List as per MVC now My Working Code is below:

    HTML+MVC Code in View:-

        
            @Html.Label("Country")
            @Html.DropDownListFor(x =>x.Province,SelectListItemHelper.GetCountryList())*
        
        
            @Html.LabelFor(x=>x.Province)
            @Html.DropDownListFor(x =>x.Province,SelectListItemHelper.GetProvincesList())*
        
    

    Created a Controller under "UTIL" folder: Code:-

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    
    namespace MedAvail.Applications.MedProvision.Web.Util
    {
        public class SelectListItemHelper
        {
            public static IEnumerable GetProvincesList()
            {
                IList items = new List
                {
                    new SelectListItem{Text = "California", Value = "B"},
                    new SelectListItem{Text = "Alaska", Value = "B"},
                    new SelectListItem{Text = "Illinois", Value = "B"},
                    new SelectListItem{Text = "Texas", Value = "B"},
                    new SelectListItem{Text = "Washington", Value = "B"}
    
                };
                return items;
            }
    
    
            public static IEnumerable GetCountryList()
            {
                IList items = new List
                {
                    new SelectListItem{Text = "United States", Value = "B"},
                    new SelectListItem{Text = "Canada", Value = "B"},
                    new SelectListItem{Text = "United Kingdom", Value = "B"},
                    new SelectListItem{Text = "Texas", Value = "B"},
                    new SelectListItem{Text = "Washington", Value = "B"}
    
                };
                return items;
            }
    
    
        }
    }
    

    And its working COOL now :-)

    Thank you!!

提交回复
热议问题