如何绑定数据表为@ Html.DropDownListFor在asp.net MVC3 [英] How to bind DataTable to @Html.DropDownListFor in asp.net MVC3

查看:125
本文介绍了如何绑定数据表为@ Html.DropDownListFor在asp.net MVC3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataTable,它看起来像如下:

TOTAL_ code COD_NAME
AP0001学校
AP0002医院
AP0003机场
AP0004众议院

我很新的ASP.NET MVC3,我无法弄清楚如何将数据表绑定到我的DropDownListFor控制。

PS:我有如下模式:

  @model Kery.Models.Profile
@ {
    ViewBag.Title =细节加入;
    布局=〜/查看/共享/ _Layout.cshtml
}< P>请选择您的性别类型:@ Html.DropDownListFor(.............)LT; / P>

型号:

 公共类档案
{
    公共IEnumerable的< SelectListItem> SexList {搞定;组; }
}

控制器:

  [HTTPGET]
公众的ActionResult细节加入()
{
    System.Data.DataTable _dtJikchaekList = _bp.DtReturnS(假
     CHP_AJAX_ codeHELPER
     ,JJ
     ,
     ,0
     );
    返回查看();
}


解决方案

这是做到这一点的方法之一。

 编辑: //你可以以此为示范性
            公共类性爱{
                公共字符串性别{搞定;组; }
                公共字符串短名称{搞定;组; }
            }
            公开名单< SelectListItem> SexList(){
                //如果你有你的性别模式在数据库中,你可以在这里                //我有低于静态内容,只是为了显示你如何manuplate性模型,
                清单<性别> S =新的List<性别>(){新的性别(){性别=男,短名称=M},新的性别(){性别=女,短名称=F}};                清单< SelectListItem>项目=新的List< SelectListItem>();
                //通过性模型,并填充你选择列表项
                的foreach(在性爱性爱){
                    SelectListItem项目=新SelectListItem();
                    item.Text = sex.gender;
                    item.Value = sex.shortname;
                    items.Add(项目);
                }
                返回的物品;
            }

在您的控制器

  [HTTPGET]
公众的ActionResult细节加入()
{
    个人资料个人资料=新的配置文件();
    //设置性别类型
    profile.SexList = SexList();    返回视图(配置文件);
}

这是你的看法

  @model Kery.Models.Profile
@ {
    ViewBag.Title =细节加入;
    布局=〜/查看/共享/ _Layout.cshtml
}< P>请选择您的性别类型:@ Html.DropDownListFor(名,Model.SexList)LT; / P>

I have a DataTable which looks like below:

TOTAL_CODE COD_NAME AP0001 School AP0002 Hospital AP0003 Airport AP0004 House

I am very new to ASP.NET MVC3 and I cannot figure it out how to bind a DataTable to my DropDownListFor control.

PS: I have model as below:

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

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

Model:

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

Controller:

[HttpGet]
public ActionResult DetailAdd()
{
    System.Data.DataTable _dtJikchaekList = _bp.DtReturnS(false
     , "CHP_AJAX_CODEHELPER"
     , "JJ"
     , ""
     , "0"
     );
    return View();
}

解决方案

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>

这篇关于如何绑定数据表为@ Html.DropDownListFor在asp.net MVC3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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