DropDownList in ASP.NET MVC

View Model

public class DropDownListViewModel
{
    public SelectList DistrictSelectList { get; set; }

    [Display(Name = "District")]
    public District District { get; set; }
}

Controller

public ActionResult DropDownList()
{
    DropDownListViewModel model = new DropDownListViewModel();
    model.DistrictSelectList = new SelectList(_entities.Districts, "DistrictId", "Name");

    return View(model);
}

View

@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()
    @Html.LabelFor(m => m.District) <text>:</text>
    @Html.DropDownListFor(m => m.District, Model.DistrictSelectList, "-- Please Select --")
    @Html.ValidationMessageFor(m => m.District)
    <button type="submit">Submit</button>
}

References

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s