Razor, ASP.Net, user authentication - asp.net-mvc-3

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>
}
}

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.

jqwidgets angular tree widget not working in jsfiddle

I'm working with the new jqwidgets-angular version, released just last month in September.
The gridtree is working great, but having trouble with the tree widget.
FYI: This is based on the angular-tree demo here: http://www.jqwidgets.com/jquery-widgets-demo/demos/jqxangular/index.htm#demos/jqxangular/tree.htm
Here's the jsfiddle I'm working on, but cannot get tree to render here: tree widget
So two things:
1) Can someone help me get this jsfiddle working ?
2) Can someone help me get the jqx-tree to render using ng-repeat
Here's the code from that jsfiddle.
Just a straight ng-repeat inside a div (no tree widget). This is just for testing purposes:
<div ng-app="App" ng-controller="myController">
<div>
<ul>
<li ng-repeat="kriGroup in kriData">{{kriGroup.group}}</li>
</ul>
</div>
</div>
And here's the jqx-tree directive, which is NOT working in jsfiddle example :
<div ng-controller="myController">
<jqx-tree jqx-width="'300px'" jqx-height="'300px'">
<ul>
<li id='home'>Home</li>
<li item-expanded='true'>Solutions
<ul>
<li>Education</li>
<li>Financial services</li>
<li>Government</li>
<li>Manufacturing</li>
<li>Solutions
<ul>
<li>Consumer photo and video</li>
<li>Mobile</li>
<li>Rich Internet applications</li>
<li>Technical communication</li>
<li>Training and eLearning</li>
<li>Web conferencing</li>
</ul>
</li>
<li>All industries and solutions</li>
</ul>
</li>
<li>Products
<ul>
<li>PC products</li>
<li>Mobile products</li>
<li>All products</li>
</ul>
</li>
<li>Support
<ul>
<li>Support home</li>
<li>Customer Service</li>
<li>Knowledge base</li>
<li>Books</li>
<li>Training and certification</li>
<li>Support programs</li>
<li>Forums</li>
<li>Documentation</li>
<li>Updates</li>
</ul>
</li>
<li>Communities
<ul>
<li>Designers</li>
<li>Developers</li>
<li>Educators and students</li>
<li>Partners</li>
<li>By resource
<ul>
<li>Labs</li>
<li>TV</li>
<li>Forums</li>
<li>Exchange</li>
<li>Blogs</li>
<li>Experience Design</li>
</ul>
</li>
</ul>
</li>
<li>Company
<ul>
<li>About Us</li>
<li>Press</li>
<li>Investor Relations</li>
<li>Corporate Affairs</li>
<li>Careers</li>
<li>Showcase</li>
<li>Events</li>
<li>Contact Us</li>
<li>Become an affiliate</li>
</ul>
</li>
</ul>
</jqx-tree>
</div>
and finally the js controller code :
angular.module('App', ['jqwidgets'])
.controller('myController', function ($scope) {
$scope.kriData = [
{
"group": "99_HSVaR",
"kris": [
{
"kri": "1D"
},
{
"kri": "1D CR"
},
{
"kri": "1D EQ"
},
{
"kri": "1D EUR/USD"
}
]
},
{
"group": "Additive",
"kris": [
{
"kri": "MCS"
}
]
},
{
"group": "AsianCrisis",
"kris": [
{
"kri": "Stressed"
}
]
}
];
})
I found the solution, posted in this jsfiddle: http://jsfiddle.net/robertmazzo/s9snLu9m/3/
My ng-repeat was in the wrong scope. And in addition, there were some key attributes missing such as the jqx-data directive.
For example, as shown in the jsfiddle :
<div ng-app="App" ng-controller="myController">
<jqx-tree jqx-data="kriData" jqx-height="300">
<ul>
<li ng-repeat="kriGroup in data">{{kriGroup.group}}</li>
</ul>
</jqx-tree>
</div>

Always seeing the "logged in menu"

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){...}

Razor Syntax Not working the way I expected

having some trouble with my razor syntax
gives a Parsor error saying that
The foreach block is missing a closing "}" character
<ul>
#{var client = "null";}
#foreach (var instance in Model)
{
if (instance.tbl_Policy.tbl_Client.txt_clientName != client)
{
client = instance.tbl_Policy.tbl_Client.txt_clientName;
</ul><h1>#client</h1>
<ul>
}
<li>
#instance.tbl_Policy.txt_policyNumber -
Assigned to : #instance.aspnet_Membership.aspnet_User.UserName
#instance.ATLCheckType.Question
<button type="button" rel="<%:instance.ATLCheckInstanceId.ToString()%>">DelFiled</button>
<button type="button" rel="<%:instance.ATLCheckInstanceId.ToString()%>">DelLineItem</button>
</li>
}
</ul>
Razor cannot handle imbalanced HTML tags in code blocks.
Change your if block to treat the imbalanced tags as plain text:
if (instance.tbl_Policy.tbl_Client.txt_clientName != client)
{
client = instance.tbl_Policy.tbl_Client.txt_clientName;
#:</ul><h1>#client</h1>
#:<ul>
}
The code should be refactored to correctly support balanced tags
#foreach (var groupedClient in Model.GroupBy(i => i.tbl_Policy.tbl_Client.txt_clientName))
{
<ul>
<h1>#groupedClient.Key</h1>
foreach(var instance in groupedClient)
{
<li>
#instance.tbl_Policy.txt_policyNumber -
Assigned to : #instance.aspnet_Membership.aspnet_User.UserName
#instance.ATLCheckType.Question
<button type="button" rel="#instance.ATLCheckInstanceId.ToString()">DelFiled</button>
<button type="button" rel="#instance.ATLCheckInstanceId.ToString()">DelLineItem</button>
</li>
}
</ul>
}
What's with all of the <%: %> stuff in there? You need to use the # syntax.
<ul>
#{var client = "null";}
#foreach (var instance in Model)
{
if (instance.tbl_Policy.tbl_Client.txt_clientName != client)
{
client = instance.tbl_Policy.tbl_Client.txt_clientName;
</ul><h1>#client</h1>
<ul>
}
<li>
#instance.tbl_Policy.txt_policyNumber -
Assigned to : #instance.aspnet_Membership.aspnet_User.UserName
#instance.ATLCheckType.Question
<button type="button" rel="#instance.ATLCheckInstanceId.ToString()">DelFiled</button>
<button type="button" rel="#instance.ATLCheckInstanceId.ToString()">DelLineItem</button>
</li>
}
</ul>

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