MVC - View Conditions
Quote from NerdDinner ASP.NET MVC sample application
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl" %> <% if (Request.IsAuthenticated) { %> Welcome <b><%= Html.Encode(Page.User.Identity.Name) %></b>! [ <%= Html.ActionLink("Log Off", "LogOff", "Account") %> ] <% } else { %> [ <%= Html.ActionLink("Log On", "LogOn", "Account") %> ] <% } %> This is from a partial view user control called LoginStatus.ascx. As you can see, there is a condition that changes the "whole" output of the view. Is it correct. Would it be better if there was a controller evaluating this condition and then providing an appropriate partial view?
And regardless of your answer to the previous question: How can I use the latter approach in ASP.NET MVC, i.e. the parent view can invoke the controller (instead of executing the RenderPartial from the UserControl) and let it decide which partial view to render?
How about this approach:
Solution 1 :
Create an extension method on HtmlHelper that displays the "WelcomeMessage.Anonymous.aspx" or "WelcomeMessage.Authenticated.aspx" view based on the request.
<%= Html.LoginStatus() => And put these views under / Views / Shared
/Views/Shared/LoginStatus.Anonymous.ascx /Views/Shared/LoginStatus.Authenticated.ascx Solution 2 :
Just replace the if / else with the ASP.NET LoginView control in your LoginStatus.ascx
<asp:LoginView Runat="Server"> <LoggiedInTemplate> Welcome, <%= Html.Encode(Model.UserName) %>! <button>Sign Out</button> </LoggedInTemplate> <AnonymousTemplate> <button>Sign In</button> | <button>Join Now!</button> </AnonymousTemplate> </asp:LoginView> See also :
I think that if the idea changes in accordance with some condition, it will depend on the view on it. But if the condition does not change externally (ie, “Negative numbers should be red”), but the behavior (ie “If the user is registered, he / she should see the“ AUTOMATIC ”button, and not the“ ENTRANCE ”button), then the controller must decide, you can enter the level of "rendering" between the controller and the page.
I think you are doing it pretty well. Trivia such as changes in display can and should be performed in terms of.
For example, I have a main menu created by a separate ascx. There are many such small checks inside to decide which text to display and which styles apply to list items.
If there are big solutions, for example, to think about what type of rendering based on any user action, then the controller asks questions to the business logic and decides what type of return to redirect. But if this is a fairly stable user interface element in which only text and color settings have changed slightly, add the necessary logic to it.
You can also pass a separate model to your ascx with several flags that determine what and how should be displayed. Then the actual logic for setting these flags will be somewhere else at the business logic level, and your whole view will simply look at these flags and display accordingly.
Do not worry, you are doing it right.
Instead of deciding whether the user is authenticated in the view, you can do this in the controller, for example:
public ActionResult ShowAPage() { if(!HttpContext.User.Identity.IsAuthenticated) { return RedirectToRoute("ShowLoginPage") } return View(); } Then you can redirect to the login page, and not have this logic in the view, which is not really a problem section.
A really cool way to do this is to use a different main page for authenticated users than for unauthenticated users. I suppose you want to always show either login or logout, so you can use ActionFilter to change the main page depending on the user's authentication. Then your registered users can get things like navigation bars that you might want to hide from strangers without having to solve this in the display logic.
Here is one way to do this without using an action filter, but you can do all kinds of ways, one good one is to create a user controller that inherits from the controller and override the viewing methods to select the appropriate main page.
public ActionResult ShowAPage() { if(!HttpContext.User.Identity.IsAuthenticated) { return View("ShowAPageView", "LoggedInMasterPageName"); } return View("ShowAPageView", "LoggedOutMasterPageName"); } Hope this helps.