刷新_Layouts.cshtml中的partialview
我有以下部分观点:
<span>Login Status</span>
@{
if (ViewBag.UserId > 0)
{
@Html.ActionLink("Log Out", "LogOut", "Home", null);
}
else {
@Html.ActionLink("Log In", "Login", "Home", null);
}
}
这只是一个假装的登录状态区域。现在,想法是让它出现在我的 MVC 站点的母版页中,以便它出现在所有页面上。以下是我在 _Layout.cshtml 文件中呈现它的方式:
<div id="login">
@{ Html.RenderPartial("LoginStatus"); }
</div>
我想要的是,当用户单击“登录”或“注销”链接时,会执行一个操作(使用户登录) /out),然后刷新部分视图 - 但页面的其余部分保持不变。
目前,当我单击任何链接时,网站会导航到“主页/登录”或“主页/注销”。我不希望它呈现视图,我只想刷新部分视图,无论我在哪个页面上。
建议的方法是什么?
干杯。 贾斯。
I have the following partial view:
<span>Login Status</span>
@{
if (ViewBag.UserId > 0)
{
@Html.ActionLink("Log Out", "LogOut", "Home", null);
}
else {
@Html.ActionLink("Log In", "Login", "Home", null);
}
}
It's just a pretend login status area. Now, the idea is to have this appear in master page for my MVC site, so that it appears on all pages. Here's how I'm rendering it in the _Layout.cshtml file:
<div id="login">
@{ Html.RenderPartial("LoginStatus"); }
</div>
What I'd like to have is, when the user clicks either the "Log In" or "Log Out" links, an action is performed (to log the user in/out) and then the partial view is refreshed - but the the rest of the page is left alone.
At the moment, when I click any of the links, the site navigates to "Home/Login" or "Home/Logout". I don't want it to render a view, I'd like to refresh just the partial view, regardless of which page I'm on.
What is the suggested way to do this?
Cheers.
Jas.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以使用 AJAX。因此,您可以使用 jquery 不显眼地 AJAX 化这些链接,方法是订阅这些链接的点击事件并向相应的控制器操作发送 AJAX 请求,该操作将返回部分视图并更新 DOM。
You could use AJAX. So you could use jquery to unobtrusively AJAXify those links by subscribing for their click event and sending an AJAX request to the corresponding controller action which would return the partial view and update the DOM.
尝试@Ajax.ActionLink() 方法。
Try the
@Ajax.ActionLink()
method.