1

我正在使用 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);
    }

我的问题是:如何从父模型及其子模型编辑器中发布所有值?(我已经看到存在其他类似的问题,但我已经尝试了他们的解决方案,但他们似乎没有解决我的问题。)

4

2 回答 2

0

我正在测试你的问题。我认为它已经完成并且所有值都已发布。但我认为您需要提供更多信息来解决您的问题

于 2013-04-16T07:11:16.347 回答
0

经过一番尝试,这是我发现可行的解决方案:

    [HttpPost]
    public ActionResult Edit(MultiLanguageText multilanguagetext)
    {
        if (ModelState.IsValid)
        {
            db.Entry(multilanguagetext).State = EntityState.Modified;
            foreach (var local in multilanguagetext.LocalizedTexts) db.Entry(local).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Index");
        }
        return View(multilanguagetext);
    }
于 2013-04-20T23:42:26.550 回答