ASP.NET MVC 3中没有foreach的DropDownListFor [英] DropDownListFor without foreach in ASP.NET MVC 3

查看:90
本文介绍了ASP.NET MVC 3中没有foreach的DropDownListFor的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ViewCode

public IList<Domain.Entity.Site> Sites { get; set; }

控制器(GetAll返回一个IList)

Controller (GetAll return a IList)

newViewModel.Sites = siteRepository.GetAll();

查看

@Html.DropDownListFor ?

我需要显示一个带有列表项某些属性的下拉列表. Id和Url是列表中这些项目的一些属性.

I need to display a dropdown with some properties of the list items. Id and Url are some properties of these items in the list.

推荐答案

有效!

ViewModel

ViewModel

public IEnumerable<SelectListItem> Sites {get; set;}
public string SiteSelected {get; set;}

控制器

private IEnumerable<SelectListItem> GetAllSites()
{
    return context.SITE.Select(x => new SelectListItem
    {
        Text = x.NAME,
        Value = SqlFunctions.StringConvert((double)x.ID).Trim()
    }).ToList();
}

siteViewModel.Sites = GetAllSites();

查看

@Html.DropDownListFor(model => model.SiteSelected , Model.Sites)

这篇关于ASP.NET MVC 3中没有foreach的DropDownListFor的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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