【问题标题】:Bind list of models as a property of model将模型列表绑定为模型的属性
【发布时间】:2013-05-01 08:44:10
【问题描述】:

我有一个以强类型视图发送到控制器的表单。 该视图属于“shitModel”类型,其中包含“shitType”列表

型号:

public class shitModel {
   public string name;
   public List<shitType> types;
}

查看:

should contain listboxfor model.types

有什么想法吗?谢谢

【问题讨论】:

  • 列表框是指 HTML 选择框?
  • 检查HERE
  • 您提供的链接未涵盖复杂对象列表

标签: asp.net-mvc asp.net-mvc-3 asp.net-mvc-4 listbox model-binding


【解决方案1】:

如果是剃刀视图,请在视图上编写以下代码

@*i am considering you have Name and ID members in your shitType class*@
@Html.DropDownList("shitTypes", Model.types.Select(t=> new SelectListItem{ Text = t.Name, Value = t.ID}))

【讨论】:

  • 但是由于值是 id 不会只保存 List 而不是完整的shitType?
  • 您在模型中提到了 shitType :) 您可以通过任何方式实现这一目标,但 Text 和 Value 属性具有相同的 String 值。
  • 但我不需要 List,我需要 List 我没有低估你
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-01
相关资源
最近更新 更多