gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:09:59 25 4
gpt4 key购买 nike

我正在尝试在 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; } }
}
}

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

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; } }
}
}

Controller 看起来像:

    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);
}

}

正在渲染的 View 是:

@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 中创建了两个编辑器模板:

ChildTemplate.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)
{...

这些都没有任何值(value)。

任何人都可以提出可以改进哪些方面来使其发挥作用的建议吗?预先非常感谢。

最佳答案

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

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

而且我不太明白 SubChildren 的目的您的 View 模型上的属性。

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

@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来自模板内部的助手,因为您的主视图已经包含一个表单。

关于asp.net-mvc-3 - Asp.Net MVC3 Razor - 子项目列表未从编辑器发回,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8914228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com