Change asp-page attribute value based on condition in razor pages - asp.net-core

When a click on a link the pages should be displayed on userroles.Say for example if the userrole is superadmin,then the user will have access to PageA.If the userrole is admin,then the user will have access to PageB. What i tried is ,
#if (Context.Session.GetString("userrole") == "superadmin")
{
var HomepageUrl = "/PageA";
}
else
{
var HomepageUrl = "/PageB";
}
<a asp-area="" asp-page="#HomepageUrl"></a>
But im getting error like "HomepageUrl doesnot exist in the current context".
Any help would be appreciated.

You can try to use the conditional operator ?:,here is an official doc,here is a demo:
<a asp-area="" asp-page="#(Context.Session.GetString("userrole") == "superadmin"?"/PageA":"/PageB")">link</a>

Related

Inserting HTML into Razor view based on conditional value

#(item.ID == 0 ? "" : <i class="fas fa-bus"></i>)
When I try the above statement, Razor doesn't like it, It looks like valid C# code.
What do I need to do to wrap my html tags so that it will be rendered in my UI if the ID is 0?
Try to use :
#if (item.ID != 0)
{
<i class='fas fa-bus'></i>
}

Razor Pages Tag Helper with Dynamic Parameters Assigning Current Url as Href

I'm running into trouble trying to set anchor tag helper parameters dynamically and looking for some help.
I have a nav that is a view component inside the shared _Layout.cshtml page that populates departments from a model.
#model List<DepartmentModel>
<ul class="nav">
#foreach (var d in Model)
{
<li>
<a asp-page="catalog/departments" asp-route-departmentName="#d.Name" asp-route-departmentId="#d.Id">#d.Name</a>
</li>
}
</ul>
Here is the InvokeAsync() from my View Component class
public async Task<IViewComponentResult> InvokeAsync()
{
var departments = _catalogService.GetNavDepartments();
return View(departments);
}
When I first launch the page, all the hrefs are populating correctly.
"catalog/departments/department-name-1/department-id-1"
"catalog/departments/department-name-2/department-id-2"
"catalog/departments/department-name-3/department-id-3"
If I click on one of the links, like the first link for example, I go to the proper department page "catalog/departments/department-name-1/department-id-1"
However, once I click that first link and navigate to the respective page, all the nav hrefs populate to the current url "catalog/departments/department-name-1/department-id-1" instead of the originally generated hrefs. This makes it so I can't navigate to another department.
Here is my route in the Startup.cs
services.AddRazorPages().AddRazorPagesOptions(options => {
options.Conventions.AddPageRoute("/Catalog/Departments", "{dName}/{dId}");
});
Based on the convention above, it eliminates the "catalog/department" piece of the url but I added it in this description for a sense of what I'm trying to accomplish. Even if I add this template to the page that populates the "catalog/departments" url, I get the same result.
#page "{dName}/{dId}"
Can anyone help me figure out what I am missing? Thanks in advance!
******** UPDATE ********
Currently, the only way I am able to get this to work is by adding the cache tag helper.
<cache>
<ul class="nav">
#foreach (var d in Model)
{
<li>
<a asp-page="catalog/departments" asp-route-departmentName="#d.Name" asp-route-departmentId="#d.Id">#d.Name</a>
</li>
}
</ul>
</cache>
This doesn't seem like a proper fix. Seems more of a hack then anything. Anybody have any ideas? Thanks!

Sitefinity - Get Sub Library Name List Template

So I'm making a list template for Sitefinity's document widget. The documents are in a library that is located in another library and I want to display that sub library name at the top of the list. The thing is I cant get that sub library name I can only get the parents name. This will make more sense id you see the screenshots. I would like to have "Child Library" as the title, but can't figure it out.
<ul class="simpleZebraList #Model.CssClass">
#if (Model.Items.Count() > 0)
{
var firstItem = Model.Items.First();
<h4>#firstItem.Fields.Parent.Title</h4>
}
#foreach (var item in Model.Items)
{
<li>
<div class="title">#item.Fields.Title</div>
<div class="link">
<a class="cta" href="#item.Fields.MediaUrl" target="_blank">Download</a>
</div>
</li>
}
#if (Model.ShowPager)
{
#Html.Action("Index", "ContentPager", new
{
currentPage = Model.CurrentPage,
totalPagesCount = Model.TotalPagesCount.Value,
redirectUrlTemplate = ViewBag.RedirectPageUrlTemplate
})
}
You can try this:
cast firstItem.DataItem to Document and then use FolderId property to get the Id of the folder (child library). Then use LibrariesManager to get the folder by that Id and you should be able to get its name.

Razor anchor returning original view

I am working on a search form, where the user press the Print button, then it should create a plain html page for printing:
<a href="#" onclick="Print()" target="_blank" class="btn btn-primary btn-sm">
<span class="glyphicon glyphicon-print"></span>
Print
</a>
And the Print() function (of course all the relevant data is passed):
$.get("#Url.Action("PrintData")", { ... ... ... });
This calls the appropriate controller action, which returns the correct data I am expecting with return PartialView(data);
The problem is, the new tab is not loading the PrintData.cshtml, instead it returns the original search screen.
What am I missing? Any help really appreciated.
The solution:
var url = '#Url.Action("Action", "Controller")' + '?target = "_blank"';
var data = "&projectId=" + id + "&date=" + date ...
window.open(url + data);

How can you get an attribute using Selenium 2 and PHPUnit?

I'm trying to follow the URL of a hidden Log Out link. The HTML looks like this:
<li id="wp-admin-bar-logout"><a class="ab-item" href="http://www.ananda.org/wp-login.php?action=logout&_wpnonce=f633c2d0a4">Log Out</a> </li>
Getting the element is straightforward:
$link = $this->byXPath( '//*[#id="wp-admin-bar-logout"]/a' );
Can I get attributes?
You can get attributes of elements, apparently, though I couldn't find this documented anywhere.
Here's the complete solution I used:
protected function doLogout() {
$this->url('/wp-admin');
$link = $this->byXPath( '//*[#id="wp-admin-bar-logout"]/a' );
$this->url( $link->attribute('href') );
}