Click here to Skip to main content
15,911,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
I've a problem that I was unable to represent the data from the datatable to dropdownlist in mvc3.
Posted

1 solution

Here is a sample to represent datatable content in dropdownlist using ASP .NET MVC3.

Model part:
XML
public class Sex {
    public string gender { get; set; }
    public string shortname { get; set; }
}
public List<SelectListItem> SexList() {
 
    List<Sex> s = new List<Sex>() { new Sex() { gender = "Male", shortname = "M" }, new Sex() { gender = "Female", shortname = "F" } };

    List<SelectListItem> items = new List<SelectListItem>();
    foreach (Sex sex in s) {
        SelectListItem item = new SelectListItem();
        item.Text =sex.gender;
        item.Value =sex.shortname;
        items.Add(item);
    }
    return items;
}


Controller part:
C#
[HttpGet]
public ActionResult DetailAdd()
{
    Profile profile = new Profile();
    //set the sex types
    profile.SexList=SexList();

    return View(profile);
}

View part:
HTML
@model Kery.Models.Profile
@{
    ViewBag.Title = "DetailAdd";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<p>Please select your sex type: @Html.DropDownListFor("name",Model.SexList)</p>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900