我正在尝试创建一个用于编辑附件的页面。
附件模型:
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; }
}
附件ViewModel:
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 ViewModel:
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:
Edit.cshtml:
...
@Html.EditorFor(model => model.Titles, "EditLabels")
@Html.EditorFor(model => model.Descriptions, "EditLabels")
...
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>
}
}
EditLabel.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>
当我渲染它时,它会构建名称为“标题。[0] .Text”的字段,这是完美的我认为......
当我提交表单时出现问题:“标题”和“描述”未填充在AttachmentModel中......(其他所有内容都已填充)
有什么想法吗?我做错了什么?
非常感谢!!!
答案 0 :(得分:0)
迭代列表并填充每个值应该类似于:
@foreach (JSONI18NTextModel item in model)
{
Html.EditorFor(i => item.Titles, "EditLabels");
Html.EditorFor(i => item.Descriptions, "EditLabels");
}
然后我假设您只向控制器操作发布一个模型:
[HttpPost]
public ActionResult Edit(JSONI18NTextModel model)
{
// The only Model you are posting should be accessible here.
}
如果您的页面上有模型列表,那么您可能需要创建一个Details
视图,一次只能访问一个模型,并且只将该模型发布到控制器。