Asp.Net MVC3 Razor - 子项目列表未从编辑器发回

2024-05-19

我正在尝试在 MVC3 中创建一个多级编辑器。我所说的多级是指我希望能够编辑三个层次结构级别的数据(父对象、父对象的子对象和子子对象的集合)。我的模型大致如下:

namespace MvcApplication1.Models
{
    public class Parent
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Child Child { get; set; }
    }

    public class Child
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public IEnumerable<SubChild> SubChildren { get; set; }    
    }

    public class SubChild
    {
        public int Id { get; set; }
        public string Name { get; set; }
        private DateTime _date = DateTime.Now;
        public DateTime Date { get { return _date; } set { this._date = value; } }
    }
}

由于我想在一个屏幕上显示整个层次结构,因此我创建了以下视图模型:

namespace MvcApplication1.Models.ViewModels
{
    public class ParentViewModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Child Child { get; set; }
        public IEnumerable<SubChild> SubChildren { get { return Child.SubChildren; } set { this.Child.SubChildren = value; } }
    }
}

控制器看起来像:

    public class ParentController : Controller
    {
        public ActionResult MultiLevelEdit(int id)
        {
            // Simulate data retrieval 
            Parent parent = new Parent { Id = 2 , Name = "P"};
            var child = new Child { Id = 3, Name = "Cild1" };
            List<SubChild> children = new List<SubChild>();
            children.Add(new SubChild { Id = 3, Name = "S1"});
            children.Add(new SubChild { Id = 5, Name = "S 22" });
            child.SubChildren = children;
            parent.Child = child;
            // Create view model
            ParentViewModel vm = new ParentViewModel() { Id = parent.Id, Name = parent.Name, Child = parent.Child, SubChildren = parent.Child.SubChildren };

            return View(vm);
        }

    }

正在渲染的视图是:

@model MvcApplication1.Models.ViewModels.ParentViewModel

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>

    @Html.HiddenFor(model => model.Id)
    @Html.LabelFor(model => model.Name)
    @Html.EditorFor(model => model.Name)
    <hr />
    Child
    <br />
    @Html.EditorFor(model => model.Child)

    <hr />
    SubChild
    <br />
    @Html.EditorFor(model => model.SubChildren)


    <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
}

我在 Views\Shared\EditorTemplates 中创建了两个编辑器模板:

子模板.cshtml

@model MvcApplication1.Models.Child

@{
    ViewBag.Title = "Child";
}

<h2>Child</h2>

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>Child</legend>

        @Html.HiddenFor(model => model.Id)

        <div class="editor-label">
            @Html.LabelFor(model => model.Name, "Child Name")
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>
    </fieldset>
}

和 SubChildTemplate.cshtml

@model MvcApplication1.Models.SubChild

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>SubChild</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>

        <div class="editor-label">
            @Html.LabelFor(model => model.Date)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Date)
            @Html.ValidationMessageFor(model => model.Date)
        </div>
    </fieldset>
}

一切都正确呈现,但是当我尝试保存更改时,SubChildren 集合为空。 我尝试了编辑方法的两个签名:

[HttpPost]
public ActionResult MultiLevelEdit(ParentViewModel parentVM)
{...

[HttpPost]
public ActionResult MultiLevelEdit(ParentViewModel parentVM, FormCollection collection)
{...

这些都没有任何价值。

谁能建议可以改进什么来使其发挥作用? 预先非常感谢。


您的模板命名不正确。应该:

  • ~/Views/Shared/EditorTemplates/Child.cshtml
  • ~/Views/Shared/EditorTemplates/SubChild.cshtml

我也不太明白这样做的目的SubChildren您的视图模型上的属性。

您可以将其删除并在主视图中:

@model MvcApplication1.Models.ViewModels.ParentViewModel

@using (Html.BeginForm()) {
    @Html.ValidationSummary(true)
    <fieldset>

    @Html.HiddenFor(model => model.Id)
    @Html.LabelFor(model => model.Name)
    @Html.EditorFor(model => model.Name)
    <hr />
    Child
    <br />
    @Html.EditorFor(model => model.Child)

    <hr />
    SubChild
    <br />
    @Html.EditorFor(model => model.Child.SubChildren)

    <p>
        <input type="submit" value="Save" />
    </p>
    </fieldset>
}

但你最大的问题是你嵌套了 HTML<form>不允许的元素。它是无效的 HTML,会导致未定义的行为。有些值可能会被发布,有些则不会,...删除所有Html.BeginForm来自模板内部的助手,因为您的主视图已经包含一个表单。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Asp.Net MVC3 Razor - 子项目列表未从编辑器发回 的相关文章

随机推荐