如何绑定一个DataTable使用MVC RAZOR一个DropDownList? [英] How to bind a DataTable to a DropDownList using MVC RAZOR?

查看:735
本文介绍了如何绑定一个DataTable使用MVC RAZOR一个DropDownList?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的模型返回数据表的集合,如下图所示。

如何到DataTable绑定到使用MVC RAZOR一个DropDownList?

有关每个DataTable,我想创建一个表行和下拉列表吧。

我试过低于code:

 的foreach(在Model.ParameterDataSources DataTable中的dataTable)
      {
            &所述; TR>
                <第i个
                    过滤:
                < /第i
                &所述; TD>
                    @ Html.DropDownList(ddlFilterDataSource的dataTable, - 选择One--)
                < / TD>
            < / TR>
      }

如何实现这一点?

非常感谢,


解决方案

<$p$p><$c$c><http://dotnetpools.com/Article/ArticleDetiail/?articleId=48&title=Binding%20Dropdownlist%20In%20MVC3%20Using%20C#t;>##剃须刀##    @ Html.DropDownList(国家,新的SelectList(Model.CountryList,值,文本,Model.CountryList.SelectedValue),新{@id =ddlist,@data_role =无,风格=颜色:#FFF;背景色:#FFF; FONT-FAMILY:宋体,黑体,无衬线; FONT-SIZE:10px的;颜色:#333333;保证金左:的3px;宽度:100%;高度:20px的; })
##模型##     公共类somename
      {
       公众的SelectList CountryList {搞定;组; }
      }
       公共类国家
        {
            公共字符串ID {搞定;组; }
            公共字符串名称{;组; }
        }##控制器##       公众的ActionResult指数()
        {
          清单&LT;国家&GT; objcountry =新的List&LT;国家&GT;();
          objcountry = GetCountryList();
          的SelectList objlistofcountrytobind =新的SelectList(objcountry,ID,姓名,0);
          model.CountryList = objlistofcountrytobind;
          返回查看(模型);
        }
      [HttpPost]
      公众的ActionResult分析(AnalyzeModels模型)
       {
          清单&LT;国家&GT; objcountry =新的List&LT;国家&GT;();
          objcountry = GetCountryList();
          的SelectList objlistofcountrytobind =新的SelectList(objcountry,ID,姓名,0);
          model.CountryList = objlistofcountrytobind;
          返回查看(模型);
       }
              **************************************************
对于GetCountryList ##功能##
        公开名单&LT;国家&GT; GetCountryList()
        {
            数据表reportDetailsTable = objCommon.GetDetails(表名);            清单&LT;国家&GT; objcountrty =新的List&LT;国家&GT;();
            INT S = reportDetailsTable.Rows.Count;
            的for(int i = 0; I&LT; S;我++)
            {
                串D1 = reportDetailsTable.Rows [I] [1]的ToString();
                串D2 = reportDetailsTable.Rows [Ⅰ] [10]的ToString();
                objcountrty.Add(新国家{ID = D1,LName的= D2});
            }
            返回objcountrty;
        }

My Model returns a collection of DataTables as shown below.

How to bind a DataTable to a DropDownList using MVC RAZOR?

For each DataTable, I'd like to create a table row and a drop down list for it.

I tried the below code:

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

How to achieve this?

Many thanks,

解决方案

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

这篇关于如何绑定一个DataTable使用MVC RAZOR一个DropDownList?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆