我正在尝试创建一个页面来编辑我的附件。
附件模型:
public class Attachment
{
...
private IList<JSONI18NText> titles = new List<JSONI18NText>();
private IList<JSONI18NText> descriptions= new List<JSONI18NText>();
...
public virtual IList<JSONI18NText> Titles
{
get { return titles; }
set { this.titles = value; }
}
public virtual IList<JSONI18NText> Descriptions
{
get { return descriptions; }
set { this.descriptions= value; }
}
JSONI18NText 模型:
public class JSONI18NText
{
public int LanguageId { get; set; }
public string Text { get; set; }
}
附件视图模型:
public class AttachmentModel
{
public AttachmentModel() { }
public AttachmentModel(Attachment at) {
...
this.Titles = new List<JSONI18NTextModel>();
this.Descriptions = new List<JSONI18NTextModel>();
foreach (JSONI18NText title in at.Titles)
{
this.Titles.Add(new JSONI18NTextModel(title, "Title"));
}
foreach (JSONI18NText description in at.Descriptions)
{
this.Descriptions.Add(new JSONI18NTextModel(description, "Description"));
}
}
[Display(Name = "Title", Description = "Title of the file")]
public IList<JSONI18NTextModel> Titles { get; set; }
[Display(Name = "Description", Description = "Description of the attachment file")]
[DataType(DataType.MultilineText)]
public IList<JSONI18NTextModel> Descriptions { get; set; }
JSONI18NText 视图模型:
public class JSONI18NTextModel
{
public JSONI18NTextModel() { }
public JSONI18NTextModel(JSONI18NText jsonI18nText)
{
this.LanguageId = jsonI18nText.LanguageId;
this.Text = jsonI18nText.Text;
}
[HiddenInput(DisplayValue = false)]
public int LanguageId { get; set; }
public string Text { get; set; }
}
现在,我想要实现的是一个带有语言选项卡列表的编辑表单:例如,两个选项卡,一个用于英语,一个用于意大利语,如果您单击每个选项卡,您会读取标题和描述的输入值那种特定的语言。
一切都像魅力一样工作:我使用了一个带有两个部分视图的视图脚本,一个用于列表,另一个用于 JSONI18NTextModel:
编辑.cshtml:
...
@Html.EditorFor(model => model.Titles, "EditLabels")
@Html.EditorFor(model => model.Descriptions, "EditLabels")
...
编辑标签.cshtml:
@model List<CR2.Web.Areas.Admin.Models.JSONI18NTextModel>
@using CR2.Web.Infrastructure
@using CR2.Web.Areas.Admin.Models
@if(Model.Count() == 1)
{
@Html.EditorFor(model => model[0], "EditLabel");
}
else
{
for(int i = 0; i < Model.Count(); ++i)
{
<div>
<ul>
@Html.EditorFor(model => model[i], "EditLabel")
</ul>
</div>
}
}
编辑标签.cshtml:
@model CR2.Web.Areas.Admin.Models.JSONI18NTextModel
@using CR2.Web.Infrastructure
<li>
@Html.HiddenFor(model => model.LanguageId)
<div>
@Html.LabelWithTooltip(model => model.Text)
</div>
<div>
@Html.EditorFor(model => model.Text)
@Html.ValidationMessageFor(model => model.Text)
</div>
</li>
当我渲染它时,它会构建名称为“Titles.[0].Text”的字段,我认为这是完美的......
当我提交表单时出现问题:AttachmentModel 中没有填充“标题”和“描述”......(填充了其他所有内容)
有任何想法吗?我究竟做错了什么?
非常感谢!!!