模型中具有嵌套属性的 ASP.NET MVC DropDownListFor
我有两个课程:入门课程和范式课程。 Entry 类具有 ParadigmId 和 Paradigm 属性。所以在我看来,我有 @Model.Entry.Paradigm
。如何使用 Paradigm 模型的较新语法构建 DropDownListFor?
// Entry Model
[Bind(Exclude = "EntryId")]
public class Entry
{
[ScaffoldColumn(false)]
public int EntryId { get; set; }
.
[Display(Name = "Type")]
public int ParadigmId { get; set; }
public virtual Paradigm Paradigm { get; set; }
}
// Paradigm Model
public class Paradigm
{
[ScaffoldColumn(false)]
public int ParadigmId { get; set; }
[Required]
public string Name { get; set; }
public List<Entry> Entries { get; set; }
}
在我看来,我有 @Html.DropDownListFor(model => model.Entry.ParadigmId, model.Entry.Paradigm)
。但模型的类型是 Paradigm 而不是 IEnumerable。由于 Paradigm 是我的类的一部分(对于实体框架代码优先),我不需要使用大多数示例中列出的单独的 ViewData/ViewBag。
我在 Google 上搜索了一下,看到有人使用 Helper/Extension 方法将模型转换为 SelectList。在我的模型中使用 DropDownListFor 的最佳方法是什么?
@* Create View *@
<div class="editor-label">
@Html.LabelFor(model => model.Entry.ParadigmId)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Entry.ParadigmId, model.Entry.Paradigm)
@Html.ValidationMessageFor(model => model.Entry.ParadigmId)
</div>
I have two classes an Entry and Paradigm. The Entry class has a ParadigmId and a Paradigm property. So in my view I have @Model.Entry.Paradigm
. How do I build a DropDownListFor using the newer syntax for the Paradigm model?
// Entry Model
[Bind(Exclude = "EntryId")]
public class Entry
{
[ScaffoldColumn(false)]
public int EntryId { get; set; }
.
[Display(Name = "Type")]
public int ParadigmId { get; set; }
public virtual Paradigm Paradigm { get; set; }
}
// Paradigm Model
public class Paradigm
{
[ScaffoldColumn(false)]
public int ParadigmId { get; set; }
[Required]
public string Name { get; set; }
public List<Entry> Entries { get; set; }
}
In my view I have @Html.DropDownListFor(model => model.Entry.ParadigmId, model.Entry.Paradigm)
. But the model is of type Paradigm not IEnumerable. Since Paradigm is part of my class (for Entity Framework Code First) I do not need to use a separate ViewData/ViewBag that is listed in most examples.
I Googled a bit and saw individuals using Helper/Extension methods to convert a model into a SelectList. What is the best way to use DropDownListFor in my model?
@* Create View *@
<div class="editor-label">
@Html.LabelFor(model => model.Entry.ParadigmId)
</div>
<div class="editor-field">
@Html.DropDownListFor(model => model.Entry.ParadigmId, model.Entry.Paradigm)
@Html.ValidationMessageFor(model => model.Entry.ParadigmId)
</div>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的链接 Entry.Paradigm 延迟加载单个范式,即外键引用的范式。它不会加载数据库中的所有范式。
如果您想要拥有所有范例的下拉列表,请绑定到所选范例。然后,您将需要一个单独的 ViewBag 或 Model 属性,其中包含所有这些属性的列表。
Your link Entry.Paradigm lazy loads a single Paradigm, the one referenced by the foreign key. It does not load all the Paradigm's in the database.
If you want to have a dropdown list of all the paradigms, bound to the selected one. Then you will need a separate ViewBag or Model property that contains a list of the them all.
我一直在使用:
您使用以下方式创建:
或者,更具体地说,在您的情况下:
并在视图中渲染:
I've been using:
Which you create with:
or, more specifically in your case:
And render in the view with: