MVC 3 StackOverflowException w/ @Html.Action()
我查看了许多其他相关报告,但我的表现似乎有点不同。我正在为我的子操作返回 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能会有点错,这已经是漫长的一天了,但是在
EditProfile
中,您将PartialToLoad
(如果它为空)设置为"_EditUserInfo",然后在
_EditUserInfo
中再次将其设置为_EditUserInfo
,这不会创建一个与您所经历的行为相同的循环吗?I might be getting this a bit wrong, it's been a long day so, but in
EditProfile
you setPartialToLoad
(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?