绑定到 MVC3 中的 DropDownList

发布于 2024-12-02 10:05:46 字数 623 浏览 4 评论 0原文

以前,我在我的模型中使用它

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

,在我的视图中使用它来绑定到 Dropdownlist,

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

但如果我想使用

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

ICode 如下所示的位置,

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

我应该在我的视图和模型中做什么?

Previously, I was using this in my model

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

and this in my view to bind to the Dropdownlist

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

but if I want to use

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

溺深海 2024-12-09 10:05:46

Cyber​​nate 的解决方案有效。您还可以这样做:

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

Cyber​​nate 的版本将进行更强的类型检查,但 SelectList 更加封装。

Cybernate's solution works. You could also do:

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

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

趴在窗边数星星i 2024-12-09 10:05:46

试试这个:

@Html.DropDownListFor(model => model.Status, 
 Model.StatusList.Select(a=> 
  new SelectListItem(){
   Text=a.ShortDescription, Value=a.Id.ToString()
  }),
 "")

Try this:

@Html.DropDownListFor(model => model.Status, 
 Model.StatusList.Select(a=> 
  new SelectListItem(){
   Text=a.ShortDescription, Value=a.Id.ToString()
  }),
 "")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文