c# – MVC .NET在强类型视图中从模型集合创建下拉列表
发布时间:2020-12-15 18:11:57 所属栏目:百科 来源:网络整理
导读:所以我有一个类似于这样的集合的视图: %@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPageIListDTO.OrganizationDTO" % OrganizationDTO看起来像这样: public OrganizationDTO{ int orgID {
所以我有一个类似于这样的集合的视图:
<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IList<DTO.OrganizationDTO>>" %> OrganizationDTO看起来像这样: public OrganizationDTO { int orgID { get; set; } string orgName { get; set; } } 我只想从使用HTML帮助器的OrganizationDTO的集合中创建一个下拉列表,但对于我来说,我无法想像出来!我这样做错了吗? 我应该使用foreach循环创建选择框吗? 解决方法
我做了一个小例子,有一个像你的模型:
public class OrganizationDTO { public int orgID { get; set; } public string orgName { get; set; } } 和控制器如: public class Default1Controller : Controller { // // GET: /Default1/ public ActionResult Index() { IList<OrganizationDTO> list = new List<OrganizationDTO>(); for (int i = 0; i < 10; i++) { list.Add(new OrganizationDTO { orgID = i,orgName = "Org " + i }); } return View(list); } } 并认为: <%= Html.DropDownListFor(m => m.First().orgID,new SelectList(Model.AsEnumerable(),"orgId","orgName")) %> (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |