MVC .NET从模型系列的强类型视图中创建下拉列表 [英] MVC .NET Create Drop Down List from Model Collection in Strongly Typed view

查看:163
本文介绍了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().orgID, new SelectList(Model.AsEnumerable(), "orgId","orgName")) %>

这篇关于MVC .NET从模型系列的强类型视图中创建下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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