views:

1488

answers:

3

I have a partial view/user control called LogOnUserControl which I display in a side bar on my site (defined in Site.Master). I also have a separate LogOn view, which also renders the LogOnUserControl.

I don't want two instances of the LogOnUserControl in the LogOn view, because it's just plain confusing, so my current thinking is to include a condition such as

// Semi-pseudocode
if (!Request.IsAuthenticated) && View.Name != "LogOn")

in the LogOnUserControl.

This feels wrong, as the partial view now knows about the LogOn view. Also, I can't find out how to get the View's name, which reinforces the feeling that I'm doing something wrong! :-)

Edit: There is the further complication that the same partial view is used for both the LogOn view and the sidebar in Site.Master.

+5  A: 

Have you considered using a different master page for your login View without the login partial in the sidebar? If you are concerned about duplication of html markup you could use nested master pages to avoid that issue.

Steve Willcock
+1. Take your master page, and replace the current call to LogOnUserControl with another ContentPlaceHolder. Then create two other master pages, "Main.master" and "Logon.master" that nest from "Site.master". Main.master will add content to the new ContentPlaceHolder, but Logon.master will not. Problem solved.
Portman
I followed Portman's comment (+1 for the detail! :-) but I'm getting an error about "Cannot find ContentPlaceHolder 'MainContent' in the master page '/Views/Shared/LogOn.Master'". I set the LogOn view to use the LogOn.master page. The missing ContentPlaceHolder is in the Site.Master, which LogOn.Master is nested from... What am I doing wrong?
alastairs
Not to worry, I followed @BlakeTaylor's answer below, which solved the problem.
alastairs
+2  A: 

You could store a flag in ViewData to indicate this. Whether you want to strong-type it or just access it directly is up to you. So on your master page you could have this:

<% if (ViewData["HideLogOnUserControl"] == "Y") { %>
    Insert HTML here
<% } else { %>
    Insert HTML here
<% } %>
Kevin Pang
+1  A: 

On the master page wrap the content of the sidebar area with content area tags and give it an id like SideBarContentArea or something. What this does is create a new content area that you can choose to override on pages based of the master and specifies default content that will show up when you do not implement in on the child pages. Now on the login page all you have to do is override the SideBarContentArea and not include the login control this time.

Bada Bing!

Blake Taylor
This solution solve the problem much more quickly and easily than the one involving nested master pages.
alastairs