我希望我能在这个功能上得到一些帮助。我已经搜索过,但我似乎无法翻译我发现的示例/教程,使其在我自己的代码中工作。
为简洁起见,我们说我有2个模型,两者之间有一对多的关系。我们说这是父母和孩子。一个父母可以有零个或多个孩子。每个孩子只能有一个父母:
namespace MyApp.Models
{
public partial class Parent
{
[Key]
public int parent_id { get; set; }
public string parent_name { get; set; }
public string parent_address { get; set; }
public ICollection<Child> Child { get; set; }
}
public partial class Child { get; set; }
{
[Key]
public int child_id { get; set; }
public int parent_id { get; set; }
public string child_name { get; set; }
public string child_allergies { get; set; }
public virtual Parent parent { get; set; }
}
}
实体在数据库中正确创建了表,并在需要的位置分配主键/外键。
我将公共字段放在viewmodel中以在我的视图中呈现它们:
using MyApp.Models;
namespace MyApp.ViewModels
{
public class ParentChildViewModel
{
public int parent_id { get; set; }
public string parent_name { get; set; }
public string parent_address { get; set; }
public string child_name { get; set; }
public string child_allergies { get; set; }
}
}
我的观点如下:
@model MyApp.ViewModels.ParentChildViewModel
@using (Html.BeginForm())
{
<div>
@Html.LabelFor(model => model.parent_name)
@Html.EditorFor(model => model.parent_name)
</div>
<div>
@Html.LabelFor(model => model.parent_address)
@Html.EditorFor(model => model.parent_address)
</div>
<table id="child_table">
@{ Html.RenderPartial("_children"); }
</table>
<div>
<button id="add">Add Child</button>
<button id="rem">Remove Child</button>
</div>
<div>
<input type="submit" value="Create" />
</div>
}
<script type="text/javascript">
$("#add").click(function () {
$.ajax({
url: "@Url.Action("BlankChRow")",
cache: false,
success: function (html) {
$("#child_table").append(html);
}
});
return false;
});
$("#rem").click(function () {
$("#child_table tbody tr:last")
.remove();
return false;
});
</script>
我为孩子创建了一个局部视图,所以我可以重复一遍:
@model MyApp.ViewModels.ParentChildViewModel
@using (Html.BeginCollectionItem("children"))
{
<tr>
<td>
<div>
@Html.LabelFor(model => model.child_name)
@Html.EditorFor(model => model.child_name)
</div>
</td>
<td>
<div>
@Html.LabelFor(model => model.child_allergies)
@Html.EditorFor(model => model.child_allergies)
</div>
</td>
</tr>
}
然后,在我的控制器中(这是我被困住的地方):
private ApplicationDbContext db = new ApplicationDbContext();
public ActionResult BlankChRow()
{
return PartialView("_children");
}
public ActionResult Create()
{
return View(new ParentChildViewModel());
}
[HttpPost]
public ActionResult Create(ParentChildViewModel pcvm)
{
var parent = new Parent()
{
parent_id = pcvm.parent_id,
parent_name = pcvm.parent_name,
parent_address = pcvm.parent_address
};
var child = new Child()
{
parent_id = pcvm.parent_id,
child_name = pcvm.child_name,
child_allergies = pcvm.child_allergies
};
if (ModelState.IsValid)
{
db.Parent.Add(parent);
db.Child.Add(child);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(pcvm);
}
我已经尝试了几种不同的方法来做到这一点......但是我无法按照我想要的方式进行操作。理想情况下,View可以显示,当他们输入Parent的数据时,他们可以添加一个或多个孩子。添加的子项在子表/实体中都是自己的记录,同时具有适当的parent_id。
感谢任何帮助。
答案 0 :(得分:0)
我发现您要为给定的父级生成子级列表,但您的viewmodel看起来不是这样。
using MyApp.Models;
namespace MyApp.ViewModels
{
public class ParentChildViewModel
{
public int parent_id { get; set; }
public string parent_name { get; set; }
public string parent_address { get; set; }
public string child_name { get; set; }
public string child_allergies { get; set; }
}
}
尝试一个包含子项列表的viewmodel。 像这样:
using MyApp.Models;
namespace MyApp.ViewModels
{
public class ParentChildViewModel
{
public int parent_id { get; set; }
public string parent_name { get; set; }
public string parent_address { get; set; }
public IEnumerable<Child> children { get; set; }
}
}
我想在这里你只想迭代列表。这就是我使用IEnumerable接口而不是IList接口的原因。
要在列表中添加子项,您可以调用&#34;创建&#34;控制器的功能并传递新子节点的parent_id。 因此,在控制器中,您可以使用应用程序上下文为数据库中的给定父级创建新子级。
完成数据库事务后,您可以创建一个新的ParentChildViewModel并使用相应的子项完成它并将其返回到视图。