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
Related
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.
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.
This is I am sure an easy question but I am having trouble figuring this out.
I want to do something like this....
#(string.IsNullOrEmpty(Model.CustomerUrl) ? "" : Click me)
This snippet doesn't work.
The mixing of the html with the razor syntax and the inclusion of quotes in the attributes of the tags is making it hard to figure out.
I love razor except figuring out this kind of stuff is really tripping me up.
I would love to just not render the following at all if the CustomerUrl was null or empty...
<p class="customerLink links">#Model.CustomerName</p>
EDIT
This is still not working for me...thanks for the suggestion though.
My issue is that the above code is ALREADY in a Razor Code Block. Here is my actual code that I cannot figure out...
EDIT NUMBER TWO - THE following code is now working
#if (Model.Count() > 0)
{
foreach (var partner in Model)
{
<li>
#Html.ActionLink(#partner.CustomerName, "Details", "Customer", new { id = Customer.AID }, null)<br />
#partner.Street<br />
//this is what i cannot figure out!!
#if(!string.IsNullOrEmpty(partner.Phone))
{
#partner.Phone#:<br />
}
#partner.Distance<br />
</li>
}
}
I preceded the nested block (the if) with the # symbol. Then the markup I had to delimit with #: Then it worked.
It seems yesterday when I tried to use a nested razor code block I got a compiler error BECAUSE I preceded it with an #. So now I am more confused than ever.
In fact...if I tried to surround my #partner.Phone with quotes like this... "#partner.Phone"#:</ br> I get another compiler error. Razor is great when it works but when it doesn't it is very confusing.
Seth
Don't do an inline if.
#if(!string.IsNullOrEmpty(Model.CustomerUrl))
{
Click me
}
'Nuff Said
#if (Model.Count() > 0)
{
Presumably before this line you had html display, so to denote to razor you're using code, you need the # symbol.
foreach (var partner in Model)
{
You're within a code block already, so the # symbol wouldn't work here.
<li>
By using an html tag, razor realizes you're displaying HTML again. All content within here is assumed to be HTML. If you want to tell Razor you have code within here, you need to use the # symbol to denote the code.
#if(!string.IsNullOrEmpty(partner.Phone))
{
#partner.Phone#:<br />
}
This is correct because you need to tell Razor you're using code again. Note if this if was directly ABOVE your list tag, you wouldn't use the # symbol here, because you don't use the # symbol when you're already within code.
"#partner.Phone" doesn't work for the same reason
if(something)
""
wouldn't work in C#. You're creating an object within code without using it.
Hope that helps explain it.
One of the suggestions that you will find on the Internet regarding conditional output and View is that the two should not be mixed together. If there is something that you need to display based on some condition then you should create an HTML Helper. However, in order to answer your question what you could do (if you don't want to bother with a helper) is something like this:
#if (!String.IsNullOrWhitespace(Model.CustomerUrl))
{
<p class="customerLink links">
#Model.CustomerName
</p>
}
So, simple question really. :)
I had this following code with ASPX View Engine:
<strong><%: ((City)Model.Location).Name %></strong>
Which renders
Los Angeles
If i do this with Razor View Engine:
<strong>#((City)Model.Location).Name</strong>
^
|
syntax highlighting stops here
It renders this:
(really huge object name must be .ToString).Name
So, the highlighting cuts off at Location), and it's treating the .Name code i have (which is a property on a City object) as pure HTML.
Why is it deeming the ) as the end of the Razor code block?
Any ideas?
Also - is there a Razor reference which has all the syntax/keywords? (it took me a while to figure out that <% Import Namespace is #using with Razor).
Thanks Guys!
Does <strong>#(((City)Model.Location).Name)</strong> work? I think it's thinking the expression contained within #() is closed.
Also, for slightly easier reading...
<strong>#( (Model.Location as City).Name )</strong>
I'm building a Ajax.ActionLink in C# which starts:
<%= Ajax.ActionLink("f lastname", ...more stuff
and I'd like there to be a new line character between the words "f" and "lastname". How can I accomplish this? I thought the special character was \n but that doesn't work, and <br> doesn't work either.
You might have to revert to doing something like:
f<br />last
And then wire in the Ajax bits manually.
Try this:
<%= Ajax.ActionLink("f<br />lastname", ...more stuff
You can't use <br /> because the ActionLink method (and indeed I believe all the html and ajax extension methods) encode the string. Thus, the output would be something like
f<br />lastname
What you could try instead would be a formatting:
<%= string.Format(Ajax.ActionLink("f{0}lastname", ...more stuff), "<br />") %>
Did you try the \r\n combination?
How about:
<%= Server.UrlDecode(Ajax.ActionLink(Server.UrlEncode("f<br/>lastname"), ...more stuff
This works for me -
<%= HttpUtility.HtmlDecode(Html.ActionLink("AOT <br/> Claim #", "actionName" ))%>
The \n used to work for me. But now it seems to be depricated. Alternitavely, you may use the NewLine method, for example:
string jay = "This is a" + Environment.NewLine + "multiline" + Environment.NewLine + "statement";
I think Andrew Hare's answer is correct. If you have slightly more complicated requirement, you do have the option to create your own AjaxHelper or HtmlHelper. This will involve creating custom extension methods that work on AjaxHelper and HtmlHelpers, by doing something like:
public static class CustomHtmlHelperExtensions
{
public static MvcHtmlString FormattedActionLink(this HtmlHelper html, ...)
{
var tagBuilder = new TagBuilder("a");
// TODO : Implementation here
// this syntax might not be exact but you get the jist of it!
return MvcHtmlString.Create(tagBuilder.ToString());
}
}
You can use dotPeek or your favorite .NET reflection tool to examine the standard extensions that come with ASP.NET MVC (e.g., ActionLink) etc to find how Microsoft has implemented most of those extension methods. They have some pretty good patterns for writing those. In the past, I have taken this approach to simplify outputting HTML in a readable manner, such as, for Google Maps or Bing Maps integration, for creating options like ActionImage e.g., #Html.ActionImage(...) or to integrate outputting Textile-formatting HTML by enabling syntax such as #Html.Textile("textile formatted string").
If you define this in a separate assembly (like I do), then remember to include this into your project references and then add it to the project's Web.config as well.
Obviously, this approach is overkill for your specific purposes, and for this reason, my vote is for Andrew Hare's approach for your specific case.
It's been several years since the question was asked, but I had trouble with it. I found the answer to be (in MVC):
Text in your ActionLink: ...ActionLink("TextLine1" + Environment.Newline + "TextLine2", ...
In the ActionLink, have a class that points to a css with this line:
whitespace: pre;
That's it. I've seen answers where they put the entire Actionline in < pre > < /pre > tags, but that caused more problems than it solved.