繁体   English   中英

在asp.net MVC3 EF4.1中创建父级时创建子级动态列表

[英]Creat dynamic list of children at time of parent creation in 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 helper的开头,此刻它已经对代码名称进行了硬编码,因为这只是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

我认为您可以使用可在此处找到的Steven Sanderson解决方案: http : //blog.stevensanderson.com/2010/01/28/editing-a-variable-length-list-aspnet-mvc-2-style/

他的助手将帮助您为要在表单中添加的每个目标添加新的输入。 我在Web应用程序中随处使用它,没有任何问题。 我可以在创建或修改模式下添加/删除项目。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM