简体   繁体   English

调用应在“布局”视图中显示的局部视图的控制器

[英]Controller that calls a partial view that should display inside the Layout view

In a standard MVC application we have _Layout.cshtml and an Index.cshtml view. 在标准MVC应用程序中,我们具有_Layout.cshtmlIndex.cshtml视图。 Now imagine if the user receives an email activation after a registration and now he/she clicks on that link. 现在想象一下,如果用户在注册后收到了一封电子邮件激活信息,现在他/她单击了该链接。

The link points to /Account/ActivateAccount and after I process the activation, I wanted to redirect the user to the Login.cshtml partial view displaying a message. 该链接指向/ Account / ActivateAccount,在处理激活之后,我想将用户重定向到显示消息的Login.cshtml部分视图。

I'm aware I can pass the message via the TempData["Message"] but what I don't know is how to redirect to a partial view and have that display inside the _Layout.cshtml instead that by itself. 我知道我可以通过TempData [“ Message”]传递消息,但是我不知道如何重定向到局部视图,并在_Layout.cshtml中显示该视图,而不是自己显示。

At least this is what is happening when I call RedirectToAction("Login", "Home") 至少当我调用RedirectToAction(“ Login”,“ Home”)时,这就是发生的情况

Note: all "home" based partial views are displaying within a < div id="divmain"> and my ideal solution would be to be able to decide what view should display inside that div, from the controller . 注意:所有基于“主”的部分视图都显示在<div id =“ divmain”>中,而我的理想解决方案是能够从控制器确定应该在该div中显示什么视图。

What's the right way to call a Partial View to be displayed from within another Controller ?? 调用部分视图从另一个Controller内部显示的正确方法是什么?

I'm not entirely sure I understand the question but I think what you're asking is if you can call a partial view from the _layout? 我不确定我是否完全理解问题,但我想您要问的是您是否可以从_layout调用局部视图? This can be done with @Html.Action() helper. 这可以通过@Html.Action()帮助器完成。 It will call out to a controller and method you specify and insert the result. 它将调出您指定的控制器和方法并插入结果。 The method you call would just be a PartialResult that returns the partial view with whatever data you need. 您调用的方法只是PartialResult,它将返回带有所需数据的局部视图。

This is in contrast to @Html.Partial() which will render the partial view in place with whatever data you provide without routing back through a controller. 这与@Html.Partial()相反,后者将使用您提供的任何数据将局部视图呈现在适当的位置,而无需通过控制器返回。

EDIT: 编辑:

To summarize the comments to this answer, it seems I misunderstood the requirement. 总结对这个答案的评论,似乎我误解了要求。 A user receives an email and clicks the link to activate their registration. 用户收到电子邮件,然后单击链接以激活其注册。 The controller action that handles that request activates the user and then it needs to redirect to something. 处理该请求的控制器操作会激活用户,然后需要将其重定向到某些内容。 In this case, there was already a partial view which would serve the purpose but it couldn't be redirected to directly for some reasons. 在这种情况下,已经有部分视图可以达到目的,但是由于某些原因而无法直接重定向到该视图。 My suggestion was to just create a new View which contained a @Html.Partial() call to basically wrap the partial in a full view page. 我的建议是仅创建一个包含@Html.Partial()调用的新View,以将部分内容基本上包装在完整视图页面中。

The action method which handled the click could then just return that view directly or, if you consider the click of the link to be a "post" since it changes the application model by validating the user, you would create a new controller action and return RedirectToAction() directly. 然后,处理单击的操作方法可以直接返回该视图,或者,如果您认为链接的单击是“发布”,因为它通过验证用户来更改应用程序模型,则您将创建一个新的控制器操作并return RedirectToAction()直接return RedirectToAction() This would be like following the Post, Redirect, Get method and would prevent some issue if the user tried to refresh the "activated" page. 这就像遵循Post,Redirect,Get方法一样,并且如果用户尝试刷新“已激活”页面,则可以避免某些问题。 It would also give more control over the URL naming. 它还将提供对URL命名的更多控制。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM