MVC .NET从模型系列的强类型视图中创建下拉列表

编程入门 行业动态 更新时间:2024-10-26 19:39:10
本文介绍了MVC .NET从模型系列的强类型视图中创建下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我有一个集合类型,像这样一个观点:

So I have a view typed with a collection like so:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IList<DTO.OrganizationDTO>>" %>

该OrganizationDTO看起来是这样的:

The OrganizationDTO looks like this:

public OrganizationDTO { int orgID { get; set; } string orgName { get; set; } }

我只是想使用HTML帮助从OrganizationDTO的集合创建一个下拉列表,但对我的生活我不能看着办吧!我要对这个错误的方式?

I simply want to create a Drop Down List from the collection of OrganizationDTO's using an HTML helper but for the life of me I cant figure it out! Am I going about this the wrong way?

我应该使用foreach循环来创建选择框?

Should I be using a foreach loop to create the select box?

推荐答案

我做了一个小例子,像你这样一个模型:

I did a small example, with a model like yours:

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()ID, new SelectList(Model.AsEnumerable(), "orgId","orgName")) %>

更多推荐

MVC .NET从模型系列的强类型视图中创建下拉列表

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

发布评论

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

>www.elefans.com

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