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

前端 未结 3 1567
粉色の甜心
粉色の甜心 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条回答
  •  -上瘾入骨i
    2021-01-27 20:37

     
    
    ## 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 objcountry = new List();
                  objcountry = GetCountryList();
                  SelectList objlistofcountrytobind = new SelectList(objcountry, "ID", "Name", 0);
                  model.CountryList = objlistofcountrytobind;       
                  return View(model);
                }
    
    
              [HttpPost]
              public ActionResult Analyze(AnalyzeModels model)
               {
                  List objcountry = new List();
                  objcountry = GetCountryList();
                  SelectList objlistofcountrytobind = new SelectList(objcountry, "ID", "Name", 0);
                  model.CountryList = objlistofcountrytobind;
                  return View(model);
               }
    
    
                **************************************************
    ## function for GetCountryList##
                public List GetCountryList()
                {
                    DataTable reportDetailsTable = objCommon.GetDetails("tablename");
    
                    List objcountrty = new List();
                    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;
                }
    

提交回复
热议问题