我正在使用 ASP.NET MVC 4,我有以下模型:
public class MultiLanguageText
{
[Key] public int Id { get; set; }
[Required] public string Name { get; set; }
public virtual ICollection<LocalizedText> LocalizedTexts { get; set; }
}
public class LocalizedText
{
[Key] public int Id { get; set; }
[ForeignKey("Language")] public int LanguageId { get; set; }
public virtual Language Language { get; set; }
[ForeignKey("MultiLanguageText")] public int MultiLanguageTextId { get; set; }
public virtual MultiLanguageText MultiLanguageText { get; set; }
public string Text { get; set; }
}
我正在尝试创建一个编辑视图,MultiLanguageText
该视图还将包含LocalizedTexts
属性中每个项目的编辑器。表格是这样的:
@using (Html.BeginForm("Edit", "MultiLanguageText", FormMethod.Post)) {
@Html.ValidationSummary(true)
<fieldset>
<legend>MultiLanguageText</legend>
@Html.HiddenFor(model => model.Id)
<div class="editor-label">
@Html.LabelFor(model => model.Name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Name)
@Html.ValidationMessageFor(model => model.Name)
</div>
@Html.EditorFor(model => model.LocalizedTexts)
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
我创建的编辑器模板(在文件中~/Views/Shared/EditorTemplates/LocalizedText.cshtml
)如下:
<fieldset>
<legend>LocalizedText</legend>
@Html.HiddenFor(model => model.Id)
<div class="editor-label">
@Html.LabelFor(model => model.LanguageId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.LanguageId)
@Html.ValidationMessageFor(model => model.LanguageId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.MultiLanguageTextId)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.MultiLanguageTextId)
@Html.ValidationMessageFor(model => model.MultiLanguageTextId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Text)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Text)
@Html.ValidationMessageFor(model => model.Text)
</div>
</fieldset>
我正在使用一些预先存在的值进行测试,当页面显示时,所有数据都正确显示在父编辑器和子编辑器中。但是,当我提交表单时,在任何子编辑器中所做的所有更改都将被忽略。
我的HttpPost
方法是这样的:
[HttpPost]
public ActionResult Edit(MultiLanguageText multilanguagetext)
{
if (ModelState.IsValid)
{
db.Entry(multilanguagetext).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
return View(multilanguagetext);
}
我的问题是:如何从父模型及其子模型编辑器中发布所有值?(我已经看到存在其他类似的问题,但我已经尝试了他们的解决方案,但他们似乎没有解决我的问题。)