razor output int to DOM and increment in same line - c#

Ok, this is really bugging me!
I'm trying to do the following in one line:
This works:
<h4>3.1.#skillSectionNumber SKILLS</h4>
{skillSectionNumber++;}
This doesn't work:
<h4>3.1.#skillSectionNumber++ SKILLS</h4>
This doesn't work:
<h4>3.1.#{Response.Write(skillSectionNumber++.ToString());} SKILLS</h4>
Cheers
My solution was this:
<h4>3.1.#(skillSectionNumber++) SKILLS</h4>
I was missing the parens.
Thanks

You can use pre-increment approach.
#{ var skillSectionNumber = 10;}
<h4> 3.1.#(++skillSectionNumber) SKILLS </h4>
It basically first increase the value of you skillSectionNumber variable and then use it (for rendering)

There isn't any need to use the Response.Write. You could just use the following:
<h4>3.1.#{(skillSectionNumber++).ToString();} SKILLS</h4>

Depending where you put the ++ will make the difference.
skillSectionNumber++ does the incrementing last, and ++skillSectionNumber does the incrementing first.
So in your scenario you can do the following:
<h4>3.1.#(++skillSectionNumber) SKILLS </h4>
I hope this helps.

Related

Not able to make a valid Regex

I am new to Regex and started playing with it a few days ago. But now I am stuck at one string.
For example:
I have this string -> <a href="http://somelink.com", example1="", example2="">
I am trying to Replace all of that string from <a to > but I want to keep the href part and the link. I have been going over this at https://regex101.com but to no avail. The Regex pattern that I am trying is <a(\s?)(?!.*?href=\".*?\").*?>. This pattern does not find anything in the string. I am using C#.
Any help would be greatly appreciated. Thanks
Update:
The actual string looks like
<a href="http://somelink.com", example1="", example2="">
and I want to remove this part
, example1="", example2=""
But then I want to keep this part
<a href="http://somelink.com">
This might work <a(?:.*?)(href=\"(.*?)\").*?>
Group(1) = href="..." and Group(2) = link
Demo https://regex101.com/r/Yz0kPc/1
Added more attributes https://regex101.com/r/Yz0kPc/2
I think you can search by this:
<a[^>]*?(href="[^"]+")[^>]*>
and replace by this:
<a $1>
Run the source
Demo

In System.Linq.Dynamic.Select(), how to deal with a null object reference in a simple way?

I am using dynamic LINQ (System.Linq.Dynamic)(you may find description here, http://dynamiclinq.azurewebsites.net/GettingStarted).
The following statement works well
Products.Select("new(ProductName, CategoryID.CategoryName as CategoryName)");
But I accidentally found when CategoryID is null, the results are empty. But I supposed it would return a record such as:
ProductName="Wine", CategoryName="" (or null).
Then I found a way to do so by
Products.Select("new(ProductName, iif(CategoryID==null,\"\",CategoryID.CategoryName) as CategoryName)");
The statement is ugly.
Do you have a better solution?
Thank you in advance,
The only thing I found is here. It isn't clear why the solution was accepted, but what I did see there is that you can do this:
"new(ProductName, iif(CategoryID==null,null,CategoryID.CategoryName) as CategoryName)"
instead of this:
"new(ProductName, iif(CategoryID==null,\"\",CategoryID.CategoryName) as CategoryName)"
It isn't any shorter, but to me it makes the code a bit more readable because you just use null instead of escaping the quotes.

asp.net mvc razor foreach loop adding id to div

I am trying to add dynamic id to div inside a foreach loop concatenated with value of variable i. It throws syntax errors. What might be the issue. Can we achieve this solution without using a for loop ?
#{int i=1;}
#foreach (var or in Model.Names)
{
<div oid="#or.Id" mode="0" oids="#or.Id" id="tr"+i>
#or.Name
</div>
i++;
}
You want to construct ID in C# segment of code. One option is to do whole construction with string format:
<div oid="#or.Id" mode="0" oids="#or.Id" id="#string.Format("tr{0}",i)">
Or id="#("tr"+i)" or id="tr#(i)"
Note that you can't do just id="tr#i" because the Razor syntax parser ignores "text#text" as it looks like a normal email address.
You can't append like this:
id="tr"+i>
It must be:
id="tr#i">
You need the #.. since it won't be able to deduce between markup and Razor at that point.
in the newly C# 6 you can directly use id="#($"tr{i}")"
for myself, none of this solutions worked but adding my #i first did work, id="#i+AnyText"
after building it, and inspecting ill get id="1+AnyText", for the
next one id="2+AnyText" and so on (im using 2013vs)..
hope that helps anyone, have a nice day.
After struggling with this for a while I found that id="#("tr"+i)" did the job for me

linq sum how to display as currency

I have the following in a razor view:
<td>#payments.Sum(p => p.Amount)</td>
I want it to display as a currency so have '$' and two decimals on the end.
I think normally you go something like {0:C}.
I don't know how to incorporate this into what I've got as the Sum does not have an overload for format.
Do I need to do this with a css class?
You would do this:
<td>#String.Format("{0:C}", payments.Sum(p => p.Amount))</td>
<td>#payments.Sum(p => p.Amount).ToString("C")</td>
I think this is a slightly more efficient call than String.Format. See this answer.

How do I set a value for an element?

I'm learning Watin and I came across an interesting piece of html code:
<label class="age_label years_old" for="total_age">
<b>34</b>
<span class="placeholder">How old are you?</span>
<span class="years_old">years old</span>
<span class="years_young">years young</span>
</label>
How do I get to 34?, Im completely lost as I'm used to selecting inputs by Id or Class. But here its not an input. Its just a html tag.
I want to SET/*REPLACE* 34 with another number using watin, but how?
WatiN doesn't have setters for tag values like what you're asking. It can easily be done through javascript though. And combining a JS call with the WatiN .GetJavascriptElementReference to get the JS element makes what you're trying to do very simple. (I'm sure there is a pure JS way to do this, but I'm better in WatiN than JS these days) As is hopefully obvious, finding the first bold tag is a very, very fragile approach and if a better approach could be taken for element location.
And, if you're using this "setter" action for testing - it is a nasty smell because you're changing something a user wouldn't be able to do.
Example code
var jsRef = myIE.ElementsWithTag("b")[0].GetJavascriptElementReference();
myIE.Eval(string.Format("{0}.innerHTML = 'fred'", jsRef));
Above tested with OP's HTML, Watin 2.1, IE9, Win7-64 => works as expected.
I'd imagine something like
var result = browserInstance.LabelField(lf => lf.GetAttributeValue("class").Contains("age_label years_old")).Children().First().Text;
If this is your only <b> tag on the page, then this might work:
IE.ElementWithTag("b", Find.First());
Good luck.

Categories