MVC 3 StackOverflowException w/ @Html.Action()

发布于 2024-12-06 04:07:57 字数 2617 浏览 1 评论 0原文

我查看了许多其他相关报告,但我的表现似乎有点不同。我正在为我的子操作返回 PartialViewResults,因此这不是递归的来源。这是我所拥有的简化版本。

// The Controller

[ChildActionOnly]
public ActionResult _EditBillingInfo()
{
    // Generate model
    return PartialView(model);
}

[HttpPost]
public ActionResult _EditBillingInfo(EditBillingInfoViewModel model)
{
    // Update billing informatoin
    var profileModel = new EditProfileViewModel()
    {
        PartialToLoad = "_EditBillingInfo"
    };

    return View("EditProfile", profileModel);
} 

[ChildActionOnly]
public ActionResult _EditUserInfo()
{
    // Generate model
    return PartialView(model);
}

[HttpPost]
public ActionResult _EditUserInfo(EditUserInfoViewModel model)
{
    // Update user informatoin
    var profileModel = new EditProfileViewModel()
    {
        PartialToLoad = "_EditUserInfo"
    };

    return View("EditProfile", profileModel);
}

public ActionResult EditProfile(EditProfileViewModel model)
{
    if (String.IsNullOrEmpty(model.PartialToLoad))
    {
        model.PartialToLoad = "_EditUserInfo";
    }

    return View(model);
}

// EditProfile View
@model UPLEX.Web.ViewModels.EditProfileViewModel

@{
    ViewBag.Title = "Edit Profile";
    Layout = "~/Views/Shared/_LoggedInLayout.cshtml";
}

<div>
    <h2>Edit Profile</h2>

    <ul>
        <li class="up one"><span>@Ajax.ActionLink("Account Information", "_EditUserInfo",
            new AjaxOptions { UpdateTargetId = "EditProfileDiv", LoadingElementId = "LoadingImage" })</span></li>
        <li class="up two"><span>@Ajax.ActionLink("Billing Information", "_EditBillingInfo",
            new AjaxOptions { UpdateTargetId = "EditProfileDiv", LoadingElementId = "LoadingImage" })</span></li>
    </ul>
    <img alt="Loading Image" id="LoadingImage" style="display: none;" src="../../Content/Images/Misc/ajax-loader.gif" />

    <div id="EditProfileDiv">
        @Html.Action(Model.PartialToLoad)
    </div>
</div>

部分视图都是用于更新用户信息或计费信息的形式。

我对此进行了调试并发现发生了什么,但无法弄清楚原因。当用户浏览到 EditProfile 时,它​​会加载 _EditUserInfo 部分,并且表单可供编辑。当您更改某些信息并提交表单时,它会挂起,并且在调用 @Html.Action() 时,您会在 EditProfile 视图中收到 StackOverflowException。首次访问 EditProfile 时,@Html.Action 会调用 _EditUserInfo 的 HttpGet 版本。您对用户信息进行一些更改,然后单击“提交”。信息更新后,EditProfile 视图将再次返回,但这次 @Html.Action 调用 _EditUserInfo 的 HttpPost 版本,该版本再次更新用户信息,再次返回 EditProfile 视图,并且 @ Html.Action 调用 _EditUserInfo 的 HttpPost 版本...您知道这是怎么回事了。为什么表单提交后它会调用 post 版本而不是像初次访问 EditProfile 时那样调用 get 版本?

感谢您的帮助!

I've looked over a bunch of other reports of this, but mine seems to be behaving a bit differently. I am returning PartialViewResults for my child actions, so that's not the source of the recursion. Here's a dumbed down version of what I have.

// The Controller

[ChildActionOnly]
public ActionResult _EditBillingInfo()
{
    // Generate model
    return PartialView(model);
}

[HttpPost]
public ActionResult _EditBillingInfo(EditBillingInfoViewModel model)
{
    // Update billing informatoin
    var profileModel = new EditProfileViewModel()
    {
        PartialToLoad = "_EditBillingInfo"
    };

    return View("EditProfile", profileModel);
} 

[ChildActionOnly]
public ActionResult _EditUserInfo()
{
    // Generate model
    return PartialView(model);
}

[HttpPost]
public ActionResult _EditUserInfo(EditUserInfoViewModel model)
{
    // Update user informatoin
    var profileModel = new EditProfileViewModel()
    {
        PartialToLoad = "_EditUserInfo"
    };

    return View("EditProfile", profileModel);
}

public ActionResult EditProfile(EditProfileViewModel model)
{
    if (String.IsNullOrEmpty(model.PartialToLoad))
    {
        model.PartialToLoad = "_EditUserInfo";
    }

    return View(model);
}

// EditProfile View
@model UPLEX.Web.ViewModels.EditProfileViewModel

@{
    ViewBag.Title = "Edit Profile";
    Layout = "~/Views/Shared/_LoggedInLayout.cshtml";
}

<div>
    <h2>Edit Profile</h2>

    <ul>
        <li class="up one"><span>@Ajax.ActionLink("Account Information", "_EditUserInfo",
            new AjaxOptions { UpdateTargetId = "EditProfileDiv", LoadingElementId = "LoadingImage" })</span></li>
        <li class="up two"><span>@Ajax.ActionLink("Billing Information", "_EditBillingInfo",
            new AjaxOptions { UpdateTargetId = "EditProfileDiv", LoadingElementId = "LoadingImage" })</span></li>
    </ul>
    <img alt="Loading Image" id="LoadingImage" style="display: none;" src="../../Content/Images/Misc/ajax-loader.gif" />

    <div id="EditProfileDiv">
        @Html.Action(Model.PartialToLoad)
    </div>
</div>

The partial views are both forms for updating either the user information or billing information.

I debugged through this and found what is happening, but cannot figure out why. When a user browses to EditProfile, it load up with the _EditUserInfo partial and the form is there for editing. When you change some info and submit the form it hangs and you get a StackOverflowException in the EditProfile view on the call to @Html.Action(). What happens is on the initial visit to EditProfile, the @Html.Action calls the HttpGet version of _EditUserInfo. You make some changes to the user info and click submit. Once the information is updated the EditProfile view is returned again, but this time @Html.Action calls the HttpPost version of _EditUserInfo which updates the user information again, returns the EditProfile view again and the @Html.Action calls the HttpPost version of _EditUserInfo... You get where this is going. Why after form submission does it call the post version and not the get version like it did for the initial visit to EditProfile?

Thanks for any help!

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

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

发布评论

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

评论(1

嘿咻 2024-12-13 04:07:57

我可能会有点错,这已经是漫长的一天了,但是在 EditProfile 中,您将 PartialToLoad (如果它为空)设置为 "_EditUserInfo",然后在 _EditUserInfo 中再次将其设置为 _EditUserInfo,这不会创建一个与您所经历的行为相同的循环吗?

I might be getting this a bit wrong, it's been a long day so, but in EditProfile you set PartialToLoad (if it's empty) to "_EditUserInfo", then in _EditUserInfo you set it again to _EditUserInfo, won't this create a loop that behaves as what you are experiencing?

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