如何在asp.net MVC3中将DataTable绑定到@ Html.DropDownListFor

问题描述 投票:0回答:3

我有一个DataTable,如下所示:

TOTAL_CODE COD_NAME AP0001学校AP0002医院AP0003机场AP0004住宅

我是ASP.NET MVC3的新手,我无法弄清楚如何将DataTable绑定到我的DropDownListFor控件。

PS:我的模型如下:

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

<P>Please select your sex type: @Html.DropDownListFor(..............)</p>

模型:

public class Profile
{
    public IEnumerable<SelectListItem> SexList { get; set; }
}

控制器:

[HttpGet]
public ActionResult DetailAdd()
{
    System.Data.DataTable _dtJikchaekList = _bp.DtReturnS(false
     , "CHP_AJAX_CODEHELPER"
     , "JJ"
     , ""
     , "0"
     );
    return View();
}
asp.net-mvc razor datatable
3个回答
2
投票

这是一种方法。

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

在你的控制器上

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

    return View(profile);
}

在你的观点

@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
投票
 <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
投票

我发现所有其他方法都非常令人困惑,所以我在我看来这样做,似乎工作得很好。零是表的列索引。我正在使用vb.net

<select>
    @Code
        For Each row In Model.dataTable.Rows
            @<option>@row(0)</option>
        Next
    End Code
</select>
© www.soinside.com 2019 - 2024. All rights reserved.