如何将数据表表示为mvc3中的dropdownlist [英] how to represent the datatable to dropdownlist in mvc3

查看:55
本文介绍了如何将数据表表示为mvc3中的dropdownlist的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个问题,我无法在mvc3中表示从数据表到下拉列表的数据. ASP .NET MVC3.

型号部分:

公共课性别{
    公共字符串性别{放; }
    公共字符串简称{get;放; }
}
公共列表<   SelectListItem  >  SexList (){
 
    List <  性别 >  s =新列表<  性别 > ( ){new Sex(){性别=男性",简称="M"},new Sex(){性别=女性",简称="F"}};

    List <   SelectListItem  >  items =新列表<   SelectListItem  > ( );
    foreach(s中的性行为){
        SelectListItem项目=新的SelectListItem();
        item.Text =性别.性别;
        item.Value = sex.shortname;
        items.Add(item);
    }
    退换货品;
} 



控制器部分:

 [HttpGet]
公共 ActionResult DetailAdd()
{
    个人资料个人资料=  Profile();
    // 设置性别类型
    profile.SexList = SexList();

    返回查看(配置文件);
} 


查看部分:

 @ model Kery.Models.Profile
@ {
    ViewBag.Title ="DetailAdd";
    布局=〜/Views/Shared/_Layout.cshtml";
}

<   p  > 请选择您的性别类型:@ Html.DropDownListFor("name",Model.SexList)<  /p  >  


hi,
I''ve a problem that I was unable to represent the data from the datatable to dropdownlist in mvc3.

解决方案

Here is a sample to represent datatable content in dropdownlist using ASP .NET MVC3.

Model part:

public class Sex {
    public string gender { get; set; }
    public string shortname { get; set; }
}
public List<SelectListItem> SexList() {
 
    List<Sex> s = new List<Sex>() { new Sex() { gender = "Male", shortname = "M" }, new Sex() { gender = "Female", shortname = "F" } };

    List<SelectListItem> items = new List<SelectListItem>();
    foreach (Sex sex in s) {
        SelectListItem item = new SelectListItem();
        item.Text =sex.gender;
        item.Value =sex.shortname;
        items.Add(item);
    }
    return items;
}



Controller part:

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

    return View(profile);
}


View part:

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

<p>Please select your sex type: @Html.DropDownListFor("name",Model.SexList)</p>


这篇关于如何将数据表表示为mvc3中的dropdownlist的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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