Optional non-boolean attribute in ASP.NET Core 7 Razor - asp.net-core

I want <li></li> or <li aria-current="page"></li> depending on some condition.
I tried:
<li #(current == Model.CurrentPage ? #"aria-current=\"page\"" : "") ></li>
Which gives the incorrect result:
<li aria-current=""page""></li>
There have been many changes in Razor over the years. In v7, is there an easy one-liner way to do this? (Without multiline if/else statements, tag helpers, etc.)
(Note that the boolean attribute rendering trick doesn't work here, as I want <li></li> rather than <li aria-current=""></li>.)

I tried as below:
<li  aria-current=#(current==Model.Page? "page":null )></li>
The result:
Is this what you want?

Related

aurelia/html repeat.for - redundant elements

I have the following html (view):
<li>
<b>Institute:</b> Length: ${institutes.length}
<ul>
<li repeat.for="el of institutes">
${el.institute}: ${el.terminalCount}
</li>
</ul>
</li>
I see the following in the browser:
As seen, the array institutes has 2 elements, but in list I see 4 more rows - with empty values.
What is it? How I can fix it?
Thanks in advance.
You definitly have something else on that array apart from the elements. Otherwise it would be just two LI tags.
If you look # the source
there are number of repeater strategies in aurelia templating. Depending on the type of the object you want to iterate over.
If you are actively developing something with aurelia, I suggest you join the official aurelia discourse
And the gitter channel

Error if don't check if {{object.field}} exists

I have a question about checking if some field in object exists.
I want to print all categories which user has so I'm doing something like this:
<ul *ngIf="user.categories.length > 0" *ngFor="#category of user.categories">
<li>
{{category.name}}
</li>
</ul>
The reason? All the data are PROPERLY printed, but I'm getting an error in web console like this:
Cannot read property 'name' of null
But when I do something like:
<ul *ngIf="user.categories.length > 0" *ngFor="#category of user.categories">
<li *ngIf="category">
{{category.name}}
</li>
</ul>
Then all is okay.
Am I doing something wrong or maybe I have to check this every time? Have you ever had a problem like this one?
basic usage
Use the safe-navigation operator
{{category?.name}}
then name is only read when category is not null.
array
This only works for the . (dereference) operator.
For an array you can use
{{records && records[0]}}
See also Angular 2 - Cannot read property '0' of undefined error with context ERROR CONTEXT: [object Object]
async pipe
With async pipe it can be used like
{{(chapters | async)?.length
ngModel
With ngModel currently it needs to be split into
[ngModel]="details?.firstname" (ngModelChange)="details.firstname = $event"
See also Data is not appending to template in angular2
*ngIf
An alternative is always to wrap the part of the view with *ngIf="data" to prevent the part being rendered at all before the data is available to prevent the dereference error.

Get xpath under particular tag

Am newbie to selenium (java).
I have to click on the menu item which is under <ul> tag as <li> items. I can able to get it with //*[#id='MainMenu']/li[5]/span xpath.
I do not want to hard code [5] of the list item, because item's position may change. It may not at 5th position all the time.
I wanted to get xpath for the particular item under particular tag with an id.
Edit:
This is how my html looks like. List item text will be loading dynamically.
<ul id="sfMainMenu" class="sf-menu ui-selectable">
<li class="ui-selected ui-selectee">
<span subnav="0" param="cmd=desk" filesrc="/Dashboard/Index"></span>
</li>
<li class="ui-selectee"></li>
<li class="ui-selectee"></li>
<li class="ui-selectee"></li>
<li class="ui-selectee">
<span subnav="18" param="cmd=desk" filesrc="../myFile.aspx"></span>
</li>
</ul>
Kindly suggest the approach with an example.
I suggest trying this:
//*[#id='MainMenu']/li[normalize-space() = 'The text you want']/span
Though if you could show us what the HTML in question actually looks like, we can provide a more reliable answer. XPath doesn't have any concept of "visual text", so if you have some text that's hidden within the li you're trying to retrieve, that could be considerably trickier.
I have solved by using filesrc attibute of span tag in the list item.
Xpath is:
//span[contains(#filesrc, 'myFile.aspx')]
As my .aspx file will be the same for any page, so I used filesrc attribute which contains the actual file name and is the only file name in that html page.
You can use the following
driver.FindElement(By.XPath("//li[contains(Text(),'Expected Text']")).Click();
Or you can avoid xpath all together by running a foreach loop on the relevant Class tag
foreach (IWebElement e in driver.FindElements(By.ClassName("ui-selectee")))
{
if (e.Displayed && e.Text == "Expected Text")
e.Click();
}

Selenium usage Findelement for list items

I need to click on a element on list item to go to next screen .
Html code for that particular element looks like
<ul>
<li id="leftSiderBarForm:tokenNoListId">
<li id="leftSiderBarForm:patientCheckinScreenId">
<li id="leftSiderBarForm:checkedInPagingId">
<li id="leftSiderBarForm:priorRegLink">
<a onclick="if(typeof jsfcljs == 'function') {jsfcljs(document.forms['leftSiderBarForm'],'leftSiderBarForm:j_id116,leftSiderBarForm:j_id116,regisType,4,registrationCategory,12','');}return false" href="#">Registration</a>
</li>
I tried using f1.findElement(By.partialLinkText("Registration")).click() not able to open locate.
Please help
Try This....
f1.findElement(By.xpath("\\a[text()='Registration']")).click();
Try like This...
f1.findElement(By.LinkText("Registration")).click();
Have you tried waiting for the element,
new WebDriverWait(f1,30).until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Registration"))).click();
It is possible, that there is another link with text Registration available, but it is either hidden or not doing the thing you expect. Anyway, try to make sure that you are getting the correct element.
You might try (from example by #user3283976, but slashed the right way):
f1.findElement(By.xpath("//a[text()='Registration']")).click();
Or
//li[#Id='leftSiderBarForm:priorRegLink']/a
Or
//a[#contains(#onclick, "registrationCategory")] // make sure that contains is unique
Also, when using double quotes and you have double quotes in your string, don't forget to escape them. Ie:
f1.findElement(By.xpath("//a[#contains(#onclick, \"registrationCategory\")]")

Zen nth element

I've been using zen coding and I love it. There's just one thing I can't figure out how to do (or if it's even possible.)
Say I typed:
ul#navigation>li*3
Which would output:
<ul id="navigation">
<li></li>
<li></li>
<li></li>
</ul>
How would I apply a class to a specific numbered element? Such as add a class called 'hello' to the second <li>?
You can‘t apply specific class for repeating element in this case.
However, you can insert specific class name (or any other attribute) by wrapping text with abbreviation. For example, you can wrap this text:
one
two
three
with the following abbreviation: ul#navigation>li[class=$#]*. It will produce the following output:
<ul class="navigation">
<li class="one"></li>
<li class="two"></li>
<li class="three"></li>
</ul>
Read more on https://github.com/sergeche/zen-coding/wiki/Release-Notes