绑定到MVC3一个DropDownList [英] Binding to a DropDownList in MVC3

查看:114
本文介绍了绑定到MVC3一个DropDownList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

previously,我在我的模型中使用此

Previously, I was using this in my model

 public List<SelectListItem> StatusList { get; set; }
 public string Status { get; set; }    

这在我看来绑定到DROPDOWNLIST

and this in my view to bind to the Dropdownlist

 @Html.DropDownListFor(model => model.Status, Model.StatusList, "")

但如果我想使用

  public List<ICode> StatusList { get; set; }

在这里我code是如下

where ICode is as below

 public interface ICode
{
    int Id { get; set; }
    ICodeGroup CodeGroup { get; set; }
    string ShortDescription { get; set; }
    string LongDescription { get; set; }
}

我应该我在视图和模型做?

What should I do in my view and model?

推荐答案

Cyber​​nate的解决方案工作。你也可以这样做:

Cybernate's solution works. You could also do:

@Html.DropDownListFor(model => model.Status, 
     new SelectList(Model.StatusList, "Id", "ShortDescription"))

Cyber​​nate的版本会做更强的类型检查,但的SelectList更加封装。

Cybernate's version will do stronger type checking, but SelectList is more encapsulated.

这篇关于绑定到MVC3一个DropDownList的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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