[英]EditorFor() for a List of Complex Type (MVC)
I'm trying to create an EditorFor() for a List of a Complex Type. 我正在尝试为复杂类型的列表创建一个EditorFor()。 Specifically the "Options" below should get displayed in a one multitext input where each option(string) is in a new line.
具体来说,下面的“选项”应显示在一个多文本输入中,其中每个选项(字符串)都在一个新行中。 However, I can only display one option in a textbox and not all options....
但是,我只能在文本框中显示一个选项而不是所有选项....
My View Model and Class: 我的视图模型和类:
public class ItemViewModel
{
public int itemId { get; set; }
[UIHint("Option")]
public List<Option> Options { get; set; }
}
public class Option
{
public string Text { get; set; }
}
My Editor Templates: 我的编辑模板:
EditorTemplates\\Item.cshtml EditorTemplates \\ Item.cshtml
@model ItemViewModel
@Html.EditorFor(model => model.Options)
EditorTemplates\\Option.cshtml EditorTemplates \\ Option.cshtml
//Not sure how to dispay the options here
<textarea rows="4" cols="50">
Display Options
</textarea>
If I update my EditorTemplates to: 如果我将EditorTemplates更新为:
EditorTemplates\\Item.cshtml EditorTemplates \\ Item.cshtml
@model ItemViewModel
@Html.EditorFor(model => model.Options[0])
EditorTemplates\\Option.cshtml EditorTemplates \\ Option.cshtml
@Html.TextBoxFor(x => x.OptionText)
It will display the first option in a textbox. 它将显示文本框中的第一个选项。 But, again what I'm trying to achieve is to display all options in a multitext input.
但是,我想要实现的是在多文本输入中显示所有选项。
Any ideas? 有任何想法吗?
Just create a view in Shared/EditorTemplates/Option.cshtml
只需在
Shared/EditorTemplates/Option.cshtml
创建一个视图
@model Option
@Html.TextBoxFor(m => m.Text)
And call 并致电
@Html.EditorFor(model => model.Options)
EditorFor iterates over collection for you. EditorFor为您迭代集合。
You nearly have it. 你几乎拥有它。
In this EditorTemplates\\Option.cshtml
add the following: 在此
EditorTemplates\\Option.cshtml
添加以下内容:
@model IEnumerable<Option>
@foreach(var option in Model)
{
@Html.TextBoxFor(m => option.Text)
}
Then call it in your view like this: 然后在你的视图中调用它,如下所示:
@Html.EditorFor(model => model.Options)
If you are not populating your options on the initial get, you will need to add this in your ItemViewModel class: 如果您没有在初始get中填充选项,则需要在ItemViewModel类中添加它:
public class ItemViewModel
{
public ItemViewModel()
{
Options = new List<Option>();
}
public int itemId { get; set; }
[UIHint("Option")]
public List<Option> Options { get; set; }
}
This constructor initializes the collection: 此构造函数初始化集合:
public ItemViewModel()
{
Options = new List<Options>();
}
I bumped into the same issue and I have different solution but a bit similar with hutchonoid. 我碰到了同样的问题,我有不同的解决方案,但有点类似于hutchonoid。
So the first part is same, modify the Option.cshtml like following: 所以第一部分是相同的,修改Option.cshtml如下:
@model IEnumerable<Option>
@foreach(var option in Model)
{
@Html.TextBoxFor(m => option.Text)
}
And in Item.cshtml, I call the Option.cshtml using Html.Partial, like following: 在Item.cshtml中,我使用Html.Partial调用Option.cshtml,如下所示:
@Html.Partial("Option", model:Model.Options)
And in my case, I don't have to modify the ItemViewModel class. 在我的情况下,我不必修改ItemViewModel类。 Hopefully this can be alternative answer for this problem.
希望这可以成为这个问题的替代答案。 Cheers!
干杯!
Using @hutchonoid 's answer, you should call the template in view: 使用@hutchonoid的答案,你应该在视图中调用模板:
@Html.EditorFor(model => Model.Options)
instead of 代替
@Html.EditorFor(model => model.Options)
and please be noted, the Option.cshtml
template is in Views\\Item\\EditorTemplates\\Option.cshtml
or View\\Shared\\EditorTemplates\\Option.cshtml
请注意,
Option.cshtml
模板位于Views\\Item\\EditorTemplates\\Option.cshtml
或View\\Shared\\EditorTemplates\\Option.cshtml
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.