在 asp.net MVC3 EF4.1 中创建父级时创建动态子级列表
我的目标是允许用户在创建和/或编辑评论时将目标(子级)添加到评论(父级)。我大部分时间都在工作,所以我正在寻找两个问题的答案:A.我怎样才能做得更好、更干净,B.当我编辑父级时,为什么它不保存新添加的子级。我的猜测是 A 的答案将使 B 过时。感谢您的帮助。
以下是一些代码亮点和我的工作临时解决方案,您可以在下面下载。
public ActionResult Create()
{
var review = new Review();
review.Goals.Add(new Goal());
return View(review);
}
//
// GET: /Reviews/Edit/5
public ActionResult Edit(int id)
{
return View(reviewRepository.AllIncluding(review => review.Goals).Single(review => review.ReviewID == id));
}
这是非常基本的,这里没有什么特别的非常基本的。
这是我的 createoredit 表单,其中包含用于 ajax 调用的 jquery
<div class="editor-label">
@Html.LabelFor(model => model.ReviewName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReviewName)
@Html.ValidationMessageFor(model => model.ReviewName)
<div id="goals">
@Html.EditorFor(model => model.Goals)
</div>
@Html.ActionLink("Add another...", "NewGoal", null, new { id = "addItem" })
<script type="text/javascript">
$("#addItem").click(function () {
$.ajax({
url: this.href,
data: "index=" + getLastIndex() + "&parentID=" + getParentID(),
cache: false,
success: function (html) {
alert(html);
$("#goals").append(html);
}
});
return false;
});
function getParentID() {
var val = $('#ReviewID').val();
if (val == null)
return 0;
else
return val;
}
function getLastIndex() {
return $('input[name=Goals.Index]:last').val();
}
</script>
ajax 请求接收到部分视图
public PartialViewResult NewGoal(int? index, int? parentID)
{
ViewBag.Index = index + 1;
ViewBag.ParentID = parentID;
return PartialView("../Shared/NewGoal", new Goal());
}
,下面是部分视图的样子。在我的项目中,你会看到我有一个 html 帮助器的开始,它使用正确的索引构建了它,它目前对模型名称进行了硬编码,因为这只是 myscratch 项目,我相信有更好的方法来做到这一点。
<input name="Goals.Index" value="0" type="hidden">
<input id="Goals_0__GoalID" name="Goals[0].GoalID" value="0" type="hidden" data-val-required="The GoalID field is required." data-val-number="The field GoalID must be a number." data-val="true">
<input id="Goals_0__ReviewID" name="Goals[0].ReviewID" value="0" type="hidden" data-val-required="The ReviewID field is required." data-val-number="The field ReviewID must be a number." data-val="true">
<div class="editor-label">
<label for="Goals_0__GoalName">GoalName</label>
</div>
<div class="editor-field">
<input id="Goals_0__GoalName" class="text-box single-line" name="Goals[0].GoalName" value="" type="text">
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Goals[0].GoalName"></span>
</div>
Visual Studio 解决方案 http://dl.dropbox.com/u/29534952/RelationScratch.zip< /a>
My objective is to allow the user to add goals(children) to a review(parent) while creating and/or editing a review. I have this mostly working so I'm looking for answers to two questions A. How can I do this better, cleaner, and B. When I edit the parent why won't it save newly added children. My guess is the answer to A will make B obsolete. Thanks for the help.
Here are some code highlights and my working scratch solution you can download below.
public ActionResult Create()
{
var review = new Review();
review.Goals.Add(new Goal());
return View(review);
}
//
// GET: /Reviews/Edit/5
public ActionResult Edit(int id)
{
return View(reviewRepository.AllIncluding(review => review.Goals).Single(review => review.ReviewID == id));
}
This is pretty basic, nothing fancy here pretty basic.
here is my createoredit form with the jquery for an ajax call
<div class="editor-label">
@Html.LabelFor(model => model.ReviewName)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ReviewName)
@Html.ValidationMessageFor(model => model.ReviewName)
<div id="goals">
@Html.EditorFor(model => model.Goals)
</div>
@Html.ActionLink("Add another...", "NewGoal", null, new { id = "addItem" })
<script type="text/javascript">
$("#addItem").click(function () {
$.ajax({
url: this.href,
data: "index=" + getLastIndex() + "&parentID=" + getParentID(),
cache: false,
success: function (html) {
alert(html);
$("#goals").append(html);
}
});
return false;
});
function getParentID() {
var val = $('#ReviewID').val();
if (val == null)
return 0;
else
return val;
}
function getLastIndex() {
return $('input[name=Goals.Index]:last').val();
}
</script>
The ajax request receives a partial view
public PartialViewResult NewGoal(int? index, int? parentID)
{
ViewBag.Index = index + 1;
ViewBag.ParentID = parentID;
return PartialView("../Shared/NewGoal", new Goal());
}
and here is what the partial looks like. In my project you'll see I have the beginnings of an html helper build up this up with the correct index, its got the model names hard coded at the moment as this is just myscratch project and I'm convinced there is a better way to do this.
<input name="Goals.Index" value="0" type="hidden">
<input id="Goals_0__GoalID" name="Goals[0].GoalID" value="0" type="hidden" data-val-required="The GoalID field is required." data-val-number="The field GoalID must be a number." data-val="true">
<input id="Goals_0__ReviewID" name="Goals[0].ReviewID" value="0" type="hidden" data-val-required="The ReviewID field is required." data-val-number="The field ReviewID must be a number." data-val="true">
<div class="editor-label">
<label for="Goals_0__GoalName">GoalName</label>
</div>
<div class="editor-field">
<input id="Goals_0__GoalName" class="text-box single-line" name="Goals[0].GoalName" value="" type="text">
<span class="field-validation-valid" data-valmsg-replace="true" data-valmsg-for="Goals[0].GoalName"></span>
</div>
Visual Studio Solution http://dl.dropbox.com/u/29534952/RelationScratch.zip
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您可以使用 Steven Sanderson 解决方案,您可以在这里找到: http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
他的助手会帮助你添加您要在表单中添加的每个目标的新输入。我在网络应用程序中到处使用它,没有任何问题。我可以在创建或修改模式下添加/删除项目。
I think that you could use Steven Sanderson solution that you can find here: http://blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/
his helper will help you add new inputs for each Goals that you want to add in your form. I use it here and there in my web applications without any problems. I can add/remove items in creation or modification mode.