ASP .NET MVC 2 - 如何使用 Ajax 将对象从视图传递到控制器?

发布于 2024-10-17 16:52:40 字数 904 浏览 1 评论 0原文

我有一个对象 MainObject,其中包含对象列表、子对象等。我试图让用户单击视图上的链接以将新的子对象添加到页面。但是,我无法将正在使用的 MainObject 传递到 Action 方法中。我当前收到的 MainObject 是空的,其所有值都设置为 null。如何将控制器操作发送到最初用于渲染视图的 MainObject?

视图的相关部分如下所示:

    <div class="editor-list" id="subObjectsList">
        <%: Html.EditorFor(model => model.SubObjects, "~/Views/MainObject/EditorTemplates/SubObjectsList.ascx")%>
    </div>
     <%: Ajax.ActionLink("Add Ajax subObject", "AddBlanksubObjectToSubObjectsList", new AjaxOptions { UpdateTargetId = "subObjectsList", InsertionMode = InsertionMode.Replace })%>

控制器的相关功能如下所示:

    public ActionResult AddBlanksubObjectToSubObjectsList(MainObject mainobject)
    {
        mainobject.SubObjects.Add(new SubObject());
        return PartialView("~/Views/MainObject/EditorTemplates/SubObjectsList.acsx", mainobject.SubObjects);
    }

I have an object MainObject with a list of objects, SubObjects, among other things. I am trying to have the user click a link on the View to add a new SubObject to the page. However, I am unable to pass the MainObject I am working with into the Action method. The MainObject I currently receive is empty, with all its values set to null. How do I send my controller action the MainObject that was used to render the View originally?

The relevant section of the view looks like this:

    <div class="editor-list" id="subObjectsList">
        <%: Html.EditorFor(model => model.SubObjects, "~/Views/MainObject/EditorTemplates/SubObjectsList.ascx")%>
    </div>
     <%: Ajax.ActionLink("Add Ajax subObject", "AddBlanksubObjectToSubObjectsList", new AjaxOptions { UpdateTargetId = "subObjectsList", InsertionMode = InsertionMode.Replace })%>

The relevant function from the controller looks like this:

    public ActionResult AddBlanksubObjectToSubObjectsList(MainObject mainobject)
    {
        mainobject.SubObjects.Add(new SubObject());
        return PartialView("~/Views/MainObject/EditorTemplates/SubObjectsList.acsx", mainobject.SubObjects);
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瑕疵 2024-10-24 16:52:40

我最终得到了以下内容:

View:

        <div class="editor-list" id="subObjectsList">
            <%: Html.EditorFor(model => model.SubObjects, "~/Views/MainObject/EditorTemplates/SubObjectsList.ascx")%>
        </div>
        <input type="button" name="addSubObject" value="Add New SubObject" onclick="AddNewSubObject('#SubObjectList')" />

Control:

 public ActionResult GetNewSubObject()
    {
        SubObject subObject= new SubObject();
        return PartialView("~/Views/TestCase/EditorTemplates/SubObject.ascx", subObject);
    }

最后,我添加了这个 JQuery 脚本:

   function AddNewSubObject(subObjectListDiv) {
        $.get("/TestCase/GetNewSubObject", function (data) {

            //there is one fieldset per SubObject already in the list,
            //so this is the index of the new SubObject
            var index = $(subObjectListDiv + " > fieldset").size();

            //the returned SubObject prefixes its field namess with "[0]."
            //but MVC expects a prefix like "SubObjects[0]" - 
            //plus the index might not be 0, so need to fix that, too
            data = data.replace(/name="\[0\]/g, 'name="SubObject[' + index + "]");

            //now append the new SubObject to the list
            $(subObjectListDiv).append(data);
        });
    }

如果有人有更好的方法来做到这一点,而不是使用 JQuery 将嵌套对象的 MVC 语法混杂到返回的 View 上,请发布它;我很愿意相信有更好的方法来做到这一点。现在,我接受我的答案。

I ended up with the following:

View:

        <div class="editor-list" id="subObjectsList">
            <%: Html.EditorFor(model => model.SubObjects, "~/Views/MainObject/EditorTemplates/SubObjectsList.ascx")%>
        </div>
        <input type="button" name="addSubObject" value="Add New SubObject" onclick="AddNewSubObject('#SubObjectList')" />

Control:

 public ActionResult GetNewSubObject()
    {
        SubObject subObject= new SubObject();
        return PartialView("~/Views/TestCase/EditorTemplates/SubObject.ascx", subObject);
    }

And, finally, I added this JQuery script:

   function AddNewSubObject(subObjectListDiv) {
        $.get("/TestCase/GetNewSubObject", function (data) {

            //there is one fieldset per SubObject already in the list,
            //so this is the index of the new SubObject
            var index = $(subObjectListDiv + " > fieldset").size();

            //the returned SubObject prefixes its field namess with "[0]."
            //but MVC expects a prefix like "SubObjects[0]" - 
            //plus the index might not be 0, so need to fix that, too
            data = data.replace(/name="\[0\]/g, 'name="SubObject[' + index + "]");

            //now append the new SubObject to the list
            $(subObjectListDiv).append(data);
        });
    }

If someone has a better way to do this than kludging the MVC syntax for nested objects onto a returned View using JQuery, please post it; I'd love to believe that there is a better way to do this. For now, I'm accepting my answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文