2012-04-17 55 views
1

My Model返回DataTable的集合,如下所示。如何使用MVC RAZOR將DataTable綁定到DropDownList?

如何使用MVC RAZOR將DataTable綁定到DropDownList?

對於每個DataTable,我想爲它創建一個表格行和一個下拉列表。

我嘗試下面的代碼:

foreach (DataTable dataTable in Model.ParameterDataSources) 
     { 
      <tr> 
       <th> 
        Filter: 
       </th> 
       <td>      
        @Html.DropDownList("ddlFilterDataSource", dataTable, "--Select One--") 
       </td> 
      </tr> 
     } 

如何實現這一目標?

+0

檢查:http://stackoverflow.com/questions/8549147/how-to-bind-datatable-to-html-dropdownlistfor-in-asp-net-mvc3 – Zaki 2012-04-17 10:47:00

+1

的鏈接通過創建視圖特定模型來建議服務器端方法。你的意思是沒有好的方法來實現這個使用RAZOR如上? – 2012-04-17 10:54:38

+0

嘗試http://knockoutjs.com/並開心。 – 2012-06-28 22:21:53

回答

2
<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; 
     } 
1

一個簡單的解決方案是將你的DataTable轉換爲DataView,你應該很好去。

//Controller Code 
    ViewBag.Employees = new SelectList(objDt.AsDataView(), "id", "name"); 
0

我發現所有其他方法非常混亂,所以我在我看來這樣做,似乎工作得很好。我使用vb.net

<select> 
    @Code 
     For Each row In Model.dataTable.Rows 
      @<option>@row(0)</option> 
     Next 
    End Code 
</select> 
相關問題