I would like links on the navigation bar to appear or disappear depending upon the data in the database. All the nav bar links are in _Layout.cshtml which is being used by all pages.
<ul>
#if (User.Identity.IsAuthenticated)
{
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#" role="button" aria-haspopup="true" aria-expanded="false">Bookings</a>
<ul class="dropdown-menu">
<li>#Html.ActionLink("All Bookings", "GetAllBookings", "Booking")</li>
<li>#Html.ActionLink("Member's Dashboard", "GetBookingsByStatus", "Booking")</li>
</ul>
</li>
</ul>
I would like member's dashboard to only appear if they are a member and have certain attributes in their profile. What is the best way to dynamically changing the nav bar links depending upon the data in database?
even in your sample code, you can just leave out the list items by wrapping them in an #if conditional and checking their permissions in whatever method you see fit.
However, I hope I'm pointing out the obvious, but this method only hides the link. It doesn't prevent the user from typing it in or using a bookmark. You will also need to duplicate the security check in your controller.
Related
i have a blazor page where i load table data depending on the data passed from the other page through the link.
the page route is defined as this #Page/approvals{tablename}
from the other page i have the links
<li class="nav-item">
<a href="approvals/customer_visitation" class="nav-link" >
<i class="far fa-circle nav-icon"></i>
<p>Customer Visitation</p>
</a>
</li>
<li class="nav-item">
<a href="approvals/mapping" class="nav-link">
<i class="far fa-circle nav-icon"></i>
<p>Mapping</p>
</a>
</li>
when i run the app and click anyone first it works well. but Navigating to the second one from the first on does not work. it shows an empty page.
my question is how do i use one page and have multiple links to that page with passed parameter?
I'm trying to select a HTML drop-down option through c# however Im not sure where to start. Could someone give a basic example.
Thanks in advance
Edited: This drop down I'm selecting is on someone's web page off the internet.
Edited: Below is the html on the webpage:
The drop down menu that contain "Ratings Data Upload" is what I am after to select.
<li class="dropdown ">
<a class="dropdown-toggle" role="button" aria-expanded="false" aria-haspopup="true" href="#" data-toggle="dropdown">
<span class="caret"></span>
</a>
<ul class="dropdown-menu">
<li class="">
Ratings Data Upload
</li>
There is a great browser automation framework called Selenium. Your next step should be using tutorials or online training content to learn Selenium (or another tool).
Selecting a drop down value with Selnium is as easy as:
Select objSelect =new Select(driver.findElement(By.id("search-box-id")));
objSelect.selectByVisibleText("Name of option shown to user");
I've got a MVC core 2 project that works fine on my development server. Once I publish it to the production server, I can't get any of my buttons to work. I can navigate to all of the pages, my Javascript and JQuery seem to work fine, but if there is a button, it will not fire upon being clicked. To be clear, navigation buttons, input buttons (or buttons used for navigation, input, etc.) will not work.
I'm thinking I'm missing some sort of library (bootstrap?), but not sure. Any ideas of what or how to go about finding what?
UPDATE
I deleted and uploaded everything again and that took care of my input buttons not working (must have had some old files left in the root); however, I still can't get my navigation buttons to work. I don't get any errors when I click on them that I can see and below is my nav code:
<div class="btn-group">
<button type="button" class="btn btn-default btn-xs dropdown-toggle" data- toggle="dropdown" aria-haspopup="true" aria-expanded="false">
DropDownTest<span class="caret" style="background-color:transparent;"></span>
</button>
<ul class="dropdown-menu" aria-labelledby="dropdownMenu2">
<li><a asp-area="" asp-controller="Home" asp-action="Something1">Something1</a></li>
<li><a asp-area="" asp-controller="Home" asp-action="Something2">Something2</a></li>
..etc
</ul>
</div>
In my ASP.NET Core project using Individual User Accounts I've made a minor change by adding a Role check to display the Register link (that, by default, is displayed on the upper right corner) to display only if the user is logged in and is in the Admin Role so that only admin, after logging, can add other users to the system as well. But for some reason, the following part not displaying the Register link even if you are logging in as an Admin who is in the AdminRole. I've checked, by placing a break point, that User.IsInRole("AdminRole") evaluates to true:
_LoginPartial:
#model ABCTest.Models.ABCTest.CurrentYear
#using Microsoft.AspNetCore.Identity
#using ABCTest.Models
#inject SignInManager<ApplicationUser> SignInManager
#inject UserManager<ApplicationUser> UserManager
#if (SignInManager.IsSignedIn(User))
{
<form asp-area="" asp-controller="Account" asp-action="LogOff" method="post" id="logoutForm">
<ul class="nav navbar-nav navbar-left">
<li><a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Welcome #UserManager.GetUserName(User), you are working with year #Model.CurrentYr - #(#Model.CurrentYr + 1)</a></li>
</ul>
<ul class="navbar-right" style="list-style:none;">
#{bool bTest = User.IsInRole("AdminRole");
if (bTest)
{
<li><a asp-area="" asp-controller="Account" asp-action="Register">Register</a></li>
}
}
<li><button type="submit" class="btn btn-link navbar-btn navbar-link">Log off</button></li>
</ul>
</form>
}
else
{
<ul class="nav navbar-nav navbar-right">
<li><a asp-area="" asp-controller="Account" asp-action="Login">Log in</a></li>
</ul>
}
Part that is not displaying even when the logged-in user is in AdminRole:
#{bool bTest = User.IsInRole("AdminRole");
if (bTest)
{
<li><a asp-area="" asp-controller="Account" asp-action="Register">Add User</a></li>
}
}
UPDATE
I've verified that user Admin is in AdminRole
When I step through the code, it takes me to the if (bTest) part that tooltip displays as true, after that I can see cursor goes to { part of if statement but then it jumps to } part of if statement. But that probably is happening since <li>....<\li> does not have the c# code inside it.
UPDATE 2:
Even if I hard code if(bTest){...} as if(true){...} the app does not display the Register link. So, I think the crux of the issue is: Why app is ignoring the Register link inside the if statement
Initially I thought it has something to do with the content inside a code block not rendering without explicit # symbol. But that shouldn't happen for html tags.
Then I thought the tag might have been rendered and is hidden by some style or just not showing due to nav bar not properly structured. Turns out that it was hidden by a logo image for OP's case.
I have the following problem: I'm making a site in Orchard and have to apply a design made by some design company. They delivered html and CSS (+ LESS) and I have to make it into a theme.
I get it done for the most part except the menu's. I want to apply a class to the nav tag in the following code, but I can't make any alternates for that end up rendering it.
<article class="widget-navigation widget-menu-widget widget" shape-id="18">
<nav shape-id="19">
<ul class="menu menu-main-menu" shape-id="19">
<li class="current first" shape-id="22">
Home
</li>
<li shape-id="24">
Something1
</li>
<li shape-id="26">
Something2
</li>
<li class="last" shape-id="28">
Something3
</li>
</ul>
</nav>
</article>
How do I influence the rendering of my menu's so that I can apply proper CSS to it? The only alternates that I can make either contain only:
#Model.Text
or:
#Display(Model.Menu)
I think you can create a custom shape for the menu, inside Views/Menu-Main.cshtml, as explained on this page. From there on, you can pretty much do whatever you want to the shape. For example
#{
// Model is Model.Menu from the layout (Layout.Menu)
var tag = Tag(Model, "ul");
var items = (IList<dynamic>)Enumerable.Cast<dynamic>(Model.Items);
if (items.Any()) {
items[0].Classes.Add("first");
items[items.Count - 1].Classes.Add("last");
}
}
<nav class='my-custom-class'>
#tag.StartElement
#DisplayChildren(Model)
#tag.EndElement
</nav>
I haven't actually tried this so apologies if I'm not 100% correct, but this should get you in the right direction at least.
Also, the sample code above is just a slightly modified version of the original menu's code, best part about Orchard is that it's open source... You can view the original code here