如何将数据表表示为mvc3中的dropdownlist

编程入门 行业动态 更新时间:2024-10-11 23:21: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

本文发布于:2023-11-30 19:28:33,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1651231.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何将   数据表   dropdownlist

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!