Tuesday, 8 March 2016

MVC - Bind Values Controlpage to DropdownList

MVC - Bind Values Control page to DropdownList 

Step1: index.cshtml

<div id="ddlcountry" class="divdrop">@Html.DropDownList("Country", ViewData["Country"] as SelectList, new { @class = "ddlall" })</div>

Step2:controller.cs

Method1:

public void FillCountry()
        {
            List<SelectListItem> objCountry = new List<SelectListItem>();
            objCountry .Add(new SelectListItem { Text = "-- Select Country--", Value = "0" });
            objCountry .Add(new SelectListItem { Text = "India", Value = "IND" });
            ViewData["Country"] = objCountry ;
        }

Method2:

public void FillCountry()
        {
            var ddList = new[] {          
            new Person { Id = 1, Name = "India" }          
            };
            Country = new SelectList(ddList, "Id", "Name");
            ViewData["Country"] = Country;
        }

No comments: