Always seeing the "logged in menu" - asp.net-mvc-3

I recently asked about why a peice of code wasn't working with regards to razor syntax, now I am back with the new question with the same piece of code:
#if(Request.LogonUserIdentity.IsAuthenticated){
<ul id="menu">
<li>#Html.ActionLink("Location", "Index", "Location")</li>
<li>#Html.ActionLink("Service", "Index", "Service")</li>
<li>#Html.ActionLink("Service Assignment", "Index", "ServiceAssignment")</li>
<li>#Html.ActionLink("Content Management", "Index", "Content")</li>
</ul>
}else{
<ul id="menu">
<li>#Html.ActionLink("Location", "Index", "Location")</li>
<li>#Html.ActionLink("Map", "Map", "Home")</li>
<li>#Html.ActionLink("Help", "Help", "Home")</li>
</ul>
}
My question this time is:
Why is it if I am logged in or out, that I only see the menu, that you should only see if you are logged in?
That is I see the authenticated users menu regardless if I am logged in or not. Is there something I need to update in the controller?

According to the MSDN, Request.LogonUserIdentity.IsAuthenticated:
Gets the WindowsIdentity type for the current user.
You most likely are using FormsAuthentication and would want to use (MSDN) Request.IsAuthenticated.
Gets a value indicating whether the request has been authenticated.
EDIT
I looked at code that we use to do this logic and we use the HttpContext.User.Identity.IsAuthenticated (MSDN)method to do this type of branching and not the Request property. Perhaps this is where your issue lies.
#if(HttpContext.Current.User.Identity.IsAuthenticated){...}

Related

How do I manipulate a menu in the View from the controller in Asp.net MVC 5?

The gist of my problem is to change the menu of my website to show the logged in user and a logout button where there was a login button from the start.
I'm new to MVC and might be going about this all wrong. this is the menu I wish to alter, and you'll see what my idea was. in Forms I could easily access this from the code behind and make one of the two menu options non visible.
<ul id="menu">
<li>#Html.ActionLink("Home", "Index", "Home")</li>
<li>#Html.ActionLink("Members", "About", "Home")</li>
<li>#Html.ActionLink("Schedule", "Contact", "Home")</li>
<li id="loginLink">#Html.ActionLink("Login", "Login", "Account")</li>
<li id="logoutLink">#Html.ActionLink("(" + Session["userName"].ToString() + ")" + "Logout", "Logout", "Account")</li>
</ul>
Why would you want to do View manipulation from the controller? I believe that View specific logic should sit in the View itself.
If you have authenticated the user, and set the authentication cookie, you could just do the following in your view:
<ul id="menu">
<li>#Html.ActionLink("Home", "Index", "Home")</li>
<li>#Html.ActionLink("Members", "About", "Home")</li>
<li>#Html.ActionLink("Schedule", "Contact", "Home")</li>
#if (User.Identity.IsAuthenticated)
{
<li id="LogoutLink">#Html.ActionLink(User.Identity.Name + "Log Out", "Logout", "Account")</li>
}
else
{
<li id="loginLink">#Html.ActionLink("Login", "Login", "Account")</li>
}
</ul>
Without knowing exactly how you authenticate your user, I cannot say for sure that this solution will suit your needs.
This example will return the LogoutLink List Item if the user is Authenticated, otherwise, it will return the LoginLink List Item if the user is not authenticated.
In case you are interested in using the ASP.NET Identity library for authentication, here is a blog with some brilliant examples, this has helped me a lot.

Razor, ASP.Net, user authentication

I am wondering why the following:
#if(Request.LogonUserIdentity.IsAnonymous){
<ul id="menu">
<li>#Html.ActionLink("Location", "Index", "Location")</li>
<li>#Html.ActionLink("Map", "Map", "Home")</li>
<li>#Html.ActionLink("Help", "Help", "Home")</li>
</ul>
#}else if(Request.LogonUserIdentity.IsAuthenticated){
<ul id="menu">
<li>#Html.ActionLink("Location", "Index", "Location")</li>
<li>#Html.ActionLink("Service", "Index", "Service")</li>
<li>#Html.ActionLink("Service Assignment", "Index", "ServiceAssignment")</li>
<li>#Html.ActionLink("Content Management", "Index", "Content")</li>
</ul>
#}
Is throwing the following error:
Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.
Compiler Error Message: CS1501: No overload for method 'Write' takes 0
arguments
This error is thrown on my else if block. I am new to ASP.net and razor, but from my understanding what I did is correct no? Since they both are Boolean returns.
I think you don't need the "#" in the else if.
Try like this:
#if(Request.LogonUserIdentity.IsAnonymous){
<ul id="menu">
<li>#Html.ActionLink("Location", "Index", "Location")</li>
<li>#Html.ActionLink("Map", "Map", "Home")</li>
<li>#Html.ActionLink("Help", "Help", "Home")</li>
</ul>
}else if(Request.LogonUserIdentity.IsAuthenticated){
<ul id="menu">
<li>#Html.ActionLink("Location", "Index", "Location")</li>
<li>#Html.ActionLink("Service", "Index", "Service")</li>
<li>#Html.ActionLink("Service Assignment", "Index", "ServiceAssignment")</li>
<li>#Html.ActionLink("Content Management", "Index", "Content")</li>
</ul>
}
Your razor syntax is goofed. You should surround the entire if/else if block with the #{} and remove the # symbol from the various closing braces in your if/else branch.
#{
if (Request.LogonUserIdentity.IsAnonymous)
{
<ul id="menu">
<li>#Html.ActionLink("Location", "Index", "Location")</li>
<li>#Html.ActionLink("Map", "Map", "Home")</li>
<li>#Html.ActionLink("Help", "Help", "Home")</li>
</ul>
}
else if (Request.LogonUserIdentity.IsAuthenticated)
{
<ul id="menu">
<li>#Html.ActionLink("Location", "Index", "Location")</li>
<li>#Html.ActionLink("Service", "Index", "Service")</li>
<li>#Html.ActionLink("Service Assignment", "Index", "ServiceAssignment")</li>
<li>#Html.ActionLink("Content Management", "Index", "Content")</li>
</ul>
}
}

Error on submiting form MVC3 .Net

Hi there I have an error when I submit the following form:
<div id="tabs">
<ul>
<li>Project Details</li>
<li>Project Attachments</li>
<li><a href="#Url.Action("Members", "ProjectNetwork", new { IsTab = true })">Project
Network</a></li>
<li>Bulleting Board</li>
<li>Bids Received</li>
</ul>
</div>
<div id="LowerButton">
#Html.Hidden("MainStatus", #Model.Status)
#using (#Html.BeginForm("Dashboard", "Dashboard"))
{
<button type="button" id="MakeComment">
Make a Comment
</button>
<input type="submit" id="GoDashBoard" value="Return to Project List" />
}
</div>
When I press the button "GoDashBoard", The method "Dashboard" in the controller "Dashboard" is not reached. Instead the following error appears:
It tells me that a model property is beign sent to the server. However, there are no model properties inside the dashboard form.. unless I'm sending many forms at the same time. But I dont think thats possible right? Do you guys have any idea of why is trying to set a model property when I'm not actually sending any?
Update:
this is the input of the dashboard action:
public ActionResult Dashboard(int page = 1)
{
var user = (User)Session["User"];
if (user != null)
{...
}}
the input is a default integer. However, I saw the trace of the calls and its submiting another form which is not related to the one im using:
That form is inside of one of the ajax tabs. I dont understand how one form submits another form and they are not nested. Anyone knows a good workaround? because im thinking of receiving both forms in both actions and make some validations.
I solved it by removing the form "Dashboard" and instead adding an invisible link. The button would reference the invisible link:
#*#using (#Html.BeginForm("Dashboard", "Dashboard"))
{ *#
<button type="button" id="MakeComment">
Make a Comment
</button>
<button name="button" type="button" onclick="document.location.href=$('#GoDashBoard').attr('href')">Return to Project List</button>
<a id="GoDashBoard" href="#Url.Action("Dashboard", "Dashboard")" style="display:none;"></a>
#*<input type="submit" id="GoDashBoard" value="Return to Project List" />*#
#* }*#

Suggestions on how to accomplish a specific functionality in MVC3

I have a MVC3 application, based on default layout from VS 2010, which I changed to looklike in image below
The submenu area is defined in _layout.cshtml as
<div id="sidebar">
<h3>Entities</h3>
<p></p>
<ul>
#Html.Partial("_EntitiesMenu")
</ul>
</div>
<section id="main">
#RenderBody()
</section>
and the _EntitiesMenu contains entries as below
<li>#Html.ActionLink("Addresses", "Index", "Address")</li>
<li>#Html.ActionLink("Applications", "Index", "Application")</li>
I have a single MapRoute defined as
routes.MapRoute("Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
{ controller = "Home", action = "Index", id = UrlParameter.Optional });
All my entity controllers started from menu are defined standard in Controllers and views in Views.
What I need is to change the app to use a layout as below
When users click Entities app should navigate to myapp/entities/ or myapp/entities/index and it should open a view in main work area that will look like below
Then when users click on right submenus, the url should look like myapp/entities/entity1/index, myapp/entities/entity1/edit/1, etc (exactly like now but "under" entities page.
I defined the Entities controller like
public class EntitiesController : Controller
{
public ActionResult Index()
{ return View();}
}
And it's view looks like
<div id="workarea">
// here should became new Body region, to load all views called from the other controllers
// something like #RenderBody(), but this don't works
</div>
<div id="sidebar">
<h3>Entities</h3>
<ul>
#Html.Partial("_EntitiesMenu")
</ul>
</div>
I don't want to have to make changes to entities controllers or views (or minimal changes if absolutely necessary, because there are lot of them). Can I somehow assign that area as main Body, while under scope of entities? And if user click on top Home / About, it will "unload" the EntitiesView from _layout.cshtml?
Not sure if my question is very clear, but I hope someone will understand what I'm after.
Thank you
Are you talking about #RenderSection http://blogs.msdn.com/b/marcinon/archive/2010/12/08/optional-razor-sections-with-default-content.aspx
I managed (sort of) to accomplish something close to what I need using following approach:
Changed the _layout as below
<section id="main">
<div>
#RenderBody()
</div>
<div>
#RenderSection("EntityCRUD", false)
</div>
</section>
Created the view for Entities as:
#Html.Partial("_PanelEntitiesMenu")
Defined _PanelEntitiesMenu as
<div id="sidebar">
<h3>Entities</h3>
<p></p>
<ul>
#Html.Partial("_EntitiesMenu")
</ul>
</div>
Enclosing the entities views (Index, Edit / Delete / Details / Create) in
#section EntityCRUD
{
#Html.Partial("_PanelEntitiesMenu")
//... original view code
}
Changed all involved views to have the view "body" included in section, and at the beginning of the section I load the panel menu as partial view
#section EntityCRUD
{
#Html.Partial("_PanelEntitiesMenu")
....
}
Not exactly what I wanted, but that's the best I found so far.

ASP.NET MVC - menu for different roles

I am writing my first MVC 3 aplication (in ASP.NET) and I don't know how I can(should) display the menu for different users.
My app is created as MVC3 Web Application and the menu look like this:
<div id="menucontainer">
<ul id="menu">
<li>#Html.ActionLink("Home", "Index", "Home")</li>
<li>#Html.ActionLink("Info", "Info", "Home")</li>
</ul>
</div>
I created two types of roles: user and admin. Now, I want to show another links for user(Projects, Profile) and for admin(Manage Projects, Manage Accounts, Manage news).
How I should do that?
I found solution:
<div id="menucontainer">
<ul id="menu">
<li>#Html.ActionLink("Home", "Index", "Home")</li>
<li>#Html.ActionLink("Info", "Info", "Home")</li>
#if ( Request.IsAuthenticated && HttpContext.Current.User.IsInRole
( "user" ) ) {
<li>Projects link</li>
<li>Profile link</li>
}
#if ( Request.IsAuthenticated && HttpContext.Current.User.IsInRole
( "admin" ) ) {
<li>Manage Projects link</li>
<li>Manage Accounts link</li>
}
</ul>
</div>

Resources