How to bind DataTable to @Html.DropDownListFor in asp.net MVC3

前端 未结 3 1564
粉色の甜心
粉色の甜心 2021-01-27 20:09

I have a DataTable which looks like below:

TOTAL_CODE COD_NAME AP0001 School AP0002 Hospital AP0003 Airport A

相关标签:
3条回答
  • 2021-01-27 20:25

    this is one way to do it.

    Edit:
    
     //you can take this as a Sex Model
                public class Sex {
                    public string gender { get; set; }
                    public string shortname { get; set; }
                }
                public List<SelectListItem> SexList() {
                    //if you have your sex model in the database , you can get it here
    
                    //I have a static content below, just to show you how you can manuplate the sex model,
                    List<Sex> s = new List<Sex>() { new Sex() { gender = "Male", shortname = "M" }, new Sex() { gender = "Female", shortname = "F" } };
    
                    List<SelectListItem> items = new List<SelectListItem>();
                    //go through the sex model and populate you selectlist items
                    foreach (Sex sex in s) {
                        SelectListItem item = new SelectListItem();
                        item.Text =sex.gender;
                        item.Value =sex.shortname;
                        items.Add(item);
                    }
                    return items;
                }
    

    On your controller

    [HttpGet]
    public ActionResult DetailAdd()
    {
        Profile profile = new Profile();
        //set the sex types
        profile.SexList=SexList();
    
        return View(profile);
    }
    

    on your view

    @model Kery.Models.Profile
    @{
        ViewBag.Title = "DetailAdd";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }
    
    <P>Please select your sex type: @Html.DropDownListFor("name",Model.SexList)</p>
    
    0 讨论(0)
  • 2021-01-27 20:29

    I found all other approaches terribly confusing, so I did this in my view and seems to be working just fine. The zero is the column index for the table. I am using vb.net

    <select>
        @Code
            For Each row In Model.dataTable.Rows
                @<option>@row(0)</option>
            Next
        End Code
    </select>
    
    0 讨论(0)
  • 2021-01-27 20:37
     <http://dotnetpools.com/Article/ArticleDetiail/?articleId=48&title=Binding%20Dropdownlist%20In%20MVC3%20Using%20C#t;>
    
    ## razor ##
    
            @Html.DropDownList("Country", new SelectList(Model.CountryList, "Value", "Text", Model.CountryList.SelectedValue), new { @id = "ddlist", @data_role = "none", style = "color: #fff; background-color: #fff; font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: #333333; margin-left:3px; width:100%; height:20px;" })
    
    
    ## model ##
    
             public class somename
              {
               public SelectList CountryList { get; set; }
              }
               public class Country
                {
                    public string ID { get; set; }
                    public string Name { get; set; }
                }
    
    
    
    ## Controller ##
    
               public ActionResult index()
                {
                  List<Country> objcountry = new List<Country>();
                  objcountry = GetCountryList();
                  SelectList objlistofcountrytobind = new SelectList(objcountry, "ID", "Name", 0);
                  model.CountryList = objlistofcountrytobind;       
                  return View(model);
                }
    
    
              [HttpPost]
              public ActionResult Analyze(AnalyzeModels model)
               {
                  List<Country> objcountry = new List<Country>();
                  objcountry = GetCountryList();
                  SelectList objlistofcountrytobind = new SelectList(objcountry, "ID", "Name", 0);
                  model.CountryList = objlistofcountrytobind;
                  return View(model);
               }
    
    
                **************************************************
    ## function for GetCountryList##
                public List<Country> GetCountryList()
                {
                    DataTable reportDetailsTable = objCommon.GetDetails("tablename");
    
                    List<Country> objcountrty = new List<Country>();
                    int s = reportDetailsTable.Rows.Count;
                    for (int i = 0; i < s; i++)
                    {
                        string d1 = reportDetailsTable.Rows[i][1].ToString();
                        string d2 = reportDetailsTable.Rows[i][10].ToString();
                        objcountrty.Add(new Country { ID = d1, LName = d2 });
                    }
                    return objcountrty;
                }
    
    0 讨论(0)
提交回复
热议问题