Restrict anonymous users to view other tabs - asp.net-mvc-3

I'm using ASP.NET MVC3. As of now I am restricting anonymous users to enter a particular page through
[Authorize]
in my controller. But what I want is to restrict them to view a tab in the main page if they are not log.in. Is this possible.? Sorry Im not pro in programming. :(

Yeah obcourse. You can do something like this
<li>#Html.ActionLink("Home", "Index", "Home")</li>
#if (Request.IsAuthenticated)
{
<li>#Html.ActionLink("View Account", "Index", "Account")</li>
if (User.IsInRole("Administrator"))
{
<li>#Html.ActionLink("Admin Console", "Index", "Admin")</li>
}
}
reference:Checking authentication and roles in a view

Related

Mvc3 implement custom action link

I have .net mvc3 web site. And i must to implement top menu. Each of the menu tags is lead to his action and display another view.This what i have now:
<ul id="menu">
<li>#Html.ActionLink("Home", "Index", "Home")</li>
<li>#Html.ActionLink("Products", "Products", "Home")</li>
<li>#Html.ActionLink("Pricing", "Pricing", "Home")</li>
<li>#Html.ActionLink("Our Team", "OurTeam", "Home")</li>
<li>#Html.ActionLink("Contact Us", "ContactUs", "Home")</li>
</ul><!-- /menu -->
This view is Html.RenderAction("Header", "Home"); in my layout(because it must to appear in all pages)
I need to implement Custom ActionLink. The anchor text must be:
<span>text</span><b>text</b>
The "text" is Home(for example of first li)
And the current ActionLink must add class :"Selected" to the anchor.
How i can do this? Help please
p.s
I can add this menu for each view in my website with "selected" class of current view, but this is not good solution.
If I understand right you want to put the "selected" class to the current displaying action.
You can do a check in the view by looking at this:
HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString()
HttpContext.Current.Request.RequestContext.RouteData.Values["action"].ToString()
Then you put the "selected" class to your actionlink the way showed in the previous answer
<li>#Html.ActionLink("Home", "Index", "Home", null, new {#class = ":selected"})</li>
Personally I will create a ViewModel containing a list of "MenuAction" that expose, other than the route values, a bool property "Selected". Imo from the view will be much more cleaner the handling of the data.
You can add class with this ActionLink overload:
<li>#Html.ActionLink("Home", "Index", "Home", null, new {#class = ":selected"})</li>
The 5th parameter is HTML attributes.
MSDN ActionLink of this overload
Update:
$(function(){
var controllerName ='ViewContext.RouteData.Values["Controller"]';
$('#' +controllerName).addClass(':selected');
});

How to send data through actionlink in asp.net?

In the application that I am trying to make, I have a menu in my view as below:
<ul id="menu">
<li>#Html.ActionLink("Create Project", "Display", "CreateDisplay")</li>
<li>#Html.ActionLink("Open", "About", "Home")</li>
</ul>
where CreateDisplay is the name of a controller and Display is a method in that controller which will call another view.
But, I want the Display method to accept certain parameters like username of the person.
In the current view, I have obtained the username as #ViewData["UserName"]
But I am not able to pass these values using ActionLink.
I tried passing the parameters as
<li>#Html.ActionLink("Create Project", "Display?UserName="+#ViewData["UserName"], "CreateDisplay")</li>
but received an exception.
Please suggest some ways of doing this.
Thank you very much in advance.
You need to use the overload that accepts parameters for route values. See the overloaded method ActionLink() here.
#Html.ActionLink(
"Click me!",
"Display",
"CreateDisplay",
new { UserName = "SetYourValHere" },
null);

If Action link is disabled, how to make it not accessible through url in MVC3

In my view I disable or enable links as below. Even if the link "Insured or Owner Name Change" is disabled in the view I am able to access it through the url like "http://localhost:0000/NameChangeRequest?contract=111111" which should not be happening. Can anyone help me on this?
#if (Model.CanCreateNameChangeRequest)
{
#Html.ActionLink("Insured or Owner Name Change", "Index", "NameChangeRequest", new { #contract = Model.ContractNumber }, new { #class = "requestLink" });
}
else
{
<span class="requestLinkDisabled">Insured or Owner Name Change</span>
}
You should never use Views to handle access control or any nature of business logic really. The View should only have presentation layer logic and markup.
That said, you can do this in the "NameChangeRequestController" -> "Index" action:
public ActionResult Index()
{
if(!Model.CanCreateNameChangeRequest)
RedirectToAction("Index","Home");
}
Also, this looks a lot like Role based permissions, in which case you should use the "[Authorize]" attribute along with "IsInRole".

No output from Html.ActionLink when trying to check Controller name

This is probably a simple oversight but I'm not seeing the problem so I thought I'd ask for some quick help. I'm somewhat new to MVC also (and Razor) so that might have something to do with it also. Basically, here's what's in my Razor View that renders some list items for a navigation bar. I'm just trying to set a class of "selected" on the element if (according to the Controller name) it's the page being requested.
<li>#{ if(Html.ViewContext.RouteData.Values["controller"].ToString() == "AdminHome")
{
Html.ActionLink("Admin Home", "Index", "AdminHome", null, new { #class = "selected" });
}
else{
Html.ActionLink("Admin Home", "Index", "AdminHome");
}
}
</li>
The result I'm getting is just an empty list item element: <li></li>
Any ideas what I'm doing wrong? Is it just a syntax issue?
You need to prefix # before Html.ActionLink. Otherwise MVC treats this as a ordinary method call not a method that outputs html.
<li>
#if(Html.ViewContext.RouteData.Values["controller"].ToString() == "AdminHome"){
#Html.ActionLink("Admin Home", "Index", "AdminHome", null, new { #class = "selected" });
}
else{
#Html.ActionLink("Admin Home", "Index", "AdminHome");
}
</li>
compare the route name in case insensitve like
String.Equals(Html.ViewContext.RouteData.Values["controller"].ToString() , "AdminHome",,StringComparison.CurrentCultureIgnoreCase)
because route name can be in any case. It is what user has typed in url.
For example in url
www.yourdomain.com/AdminHome/Index (controller name is AdminHome and controller will be AdminHomeController)
but with url
www.yourdomain.com/adminhome/index (controller name is adminhome and controller will be AdminHomeController)
Hope this helps.

Refresh partialview in _Layouts.cshtml

I have the following partial view:
<span>Login Status</span>
#{
if (ViewBag.UserId > 0)
{
#Html.ActionLink("Log Out", "LogOut", "Home", null);
}
else {
#Html.ActionLink("Log In", "Login", "Home", null);
}
}
It's just a pretend login status area. Now, the idea is to have this appear in master page for my MVC site, so that it appears on all pages. Here's how I'm rendering it in the _Layout.cshtml file:
<div id="login">
#{ Html.RenderPartial("LoginStatus"); }
</div>
What I'd like to have is, when the user clicks either the "Log In" or "Log Out" links, an action is performed (to log the user in/out) and then the partial view is refreshed - but the the rest of the page is left alone.
At the moment, when I click any of the links, the site navigates to "Home/Login" or "Home/Logout". I don't want it to render a view, I'd like to refresh just the partial view, regardless of which page I'm on.
What is the suggested way to do this?
Cheers.
Jas.
You could use AJAX. So you could use jquery to unobtrusively AJAXify those links by subscribing for their click event and sending an AJAX request to the corresponding controller action which would return the partial view and update the DOM.
Try the #Ajax.ActionLink() method.

Resources