A part of my master layout has a dropdown menu that contains a link to a modal panel for some user settings. I'm not sure how to do this, but so far I've tried the following:
UserController.cs
public class UserController : Controller
{
//
// GET: /UserProfile/
[ChildActionOnly]
public ActionResult UserProfile(string user)
{
ViewBag.UserName = user;
return View();
}
}
UserProfile.cshtml
<h4>
Modal Header
</h4>
<p class="help-block">Dummy text</p>
<hr>
@ViewBag.UserName
_Layout.cshtml
<html>
....
<head>...</head>
<body>
<div id="menu">
<li class="userlinks">
<ul class="dropdown-menu">
<li><a data-toggle="modal" href="#usrmodal">View Profile <i class="pull-right fa fa-pencil"></i></a></li>
<li>@Html.ActionLink("Sign Out", "Index", "Logout")</li>
</ul>
</li>
@RenderBody()
</body>
<!-- Modal -->
<div class="modal fade" id="usrmodal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h4 class="modal-title">User Settings</h4>
</div>
<div class="modal-body">
@{ Html.Action("UserProfile", "User", new { user = @ViewBag.UserName }); }
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
<button type="button" class="btn btn-primary">Save changes</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</html>
Whenever I try to run this I get an unhandled exception at the line in my _Layout.cshtml
where I call my @{ Html.Action("UserProfile", "User", new { user = @ViewBag.UserName }); }
@{ Html.Action("UserProfile", "User", new { user = @ViewBag.UserName }); }
:
An unhandled exception of type 'System.StackOverflowException' occurred in System.Web.Mvc.dll
How am I supposed to render views like this? Obviously I'm doing something fundamentally wrong.
View()
renders with Layout, so you get stack overflow. return PartialView();
should help you.
[ChildActionOnly]
public ActionResult UserProfile(string user)
{
ViewBag.UserName = user;
return PartialView();
}
尝试将ViewBag.UserName强制转换为字符串
@Html.Action("UserProfile", "User", new { user = (string)ViewBag.UserName });
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.