How to use a razor variable mixed with html ID text? - asp.net-mvc

I am getting an error because the razor and html is getting confused by the compiler I imagine.
<div id="#Model.MyLabelCars" ...>
My model variable is:
Model.MyLabel
The "Cars" is just raw text that should be in the HTML.
So say Model.MyLabel's value is "123" the ID should be:
id="123Car"
How can I seperate the model's variable name and HTML?

You could use regular string add operator
<div id="#(Model.MyLabel + "Car")"></div>
Or C# 6's string interpolation.
<div id="#($"{Model.MyLabel}Car")"></div>

What you want is to use the <text></text> pseudo tags
<div id="#Model.MyLabel<text>Cars</text>" ...>

Related

What does the Thymeleaf tag "th:for" do?

Anyone know that "th:for" is in Thymeleaf? I know it's a simple question, but I can't find the answer online, even in the Thymeleaf documentation.
It is the Thymeleaf attribute equivalent of the HTML for attribute used by <label> elements. For example:
<div class="preference">
<label for="cheese">Do you like cheese?</label>
<input type="checkbox" name="cheese" id="cheese">
</div>
It is listed in the Thymeleaf documentation section 5.2:
There are quite a lot of attributes like these, each of them targeting a specific HTML5 attribute...
It's no different from most other HTML attributes which have a Thymeleaf version - you can use it with a Thymeleaf expression. If you don't need a Thymeleaf expression, just use the plain for attribute instead.

How to pass data- attribute to #Html.CheckBoxFor [duplicate]

Is there a nicer syntax when creating elements with hyphenated attributes instead of using:
<%= Html.TextBox ("name", value, new Dictionary<string, object> { {"data-foo", "bar"} }) %>
Looking at the HTML specs for the proposed standards HTML 5 and WIA ARIA it seems hyphens in HTML attributes are being planned to be more common as some sort of simple name spacing.
E.g. HTML 5 proposes custom attributes are prefixed with data- and WIA ARIA uses the aria- prefix for all WIA ARIA attributes.
When using HTML helpers in ASP.NET MVC such as <%= Html.TextBox("name", value, new { attribute = attributeValue }) %> the anonymous object is converted to a dictionary.
Unfortunately in C# there is no support for hyphens in names, so the only alternative is to create a dictionary. The syntax for which is very verbose, has anyone seen a nicer alternative or a simple way of altering the functionality of ASP.NET MVC's HTML extensions without having to re-write the entire extension?
Use an underscore in the data attribute name, and it'll magically handle it for you, converting it to a hyphen. It knows you want a hyphen rather than an underscore as underscores aren't valid in html attribute names.
<%= Html.TextBox("name", value, new { #data_foo = "bar"}) %>
The answer provided at ActionLink htmlAttributes suggests using underscores instead of hyphens. MVC.Net is supposed to emit hyphens instead of the underscores when sending the page to the browser.

Passing html object properties into embedded ruby code

How can I pass html object properties into embedded ruby code in an html.erb file?
Lets say I have a ruby method A that accepts a string parameter(and also the return value of A is string). I think of scenarios like the following:
<input type="text" id="t" value="Leaves">
<%= A(document.getElementById("t").value) %>
Obviously I can't write code that way.
I want to pass the value/text of the textbox into method A and print A's return value into the html body. How can I do that?
Also, if I want to continuously check the value of the textbox and append A's return value(when passed the current value of the textbox to A) to the body of the document, what should I do? And if I instead wanted to set some paragraph p's text to this return value, what should I have done?
You can use a HTML parser like Nokogiri.
frag = Nokogiri::HTML.fragment('<input type="text" id="t" value="Leaves">')
frag.at_css('#t').attr('value')
But it seems like a rather silly and overcomplicated solution to something that most likely can be solved by not using HTML strings to pass around data in your views / helpers in the first place.

Razor, shortest conditional block

I frequently use code like:
<p #if(Model.Sth)
{
?:style="display: none;"
}>Some text</p>
Many template engines have special markers for conditional blocks, for example in Mustache you can write:
<p {{#Model.Sth}}style="display: none;"{{/Model.Sth}}>Some text</p>
Can my Razor code can be written in shorter form?
In Razor you can embed expressions inside parenthesis to execute them inline. If your conditional can be written using the ternary operator, then you can do something like this:
#(this.Model.Sth ? "style='display:none;'" : string.Empty)
The trick is getting Razor to emit the resulting string correctly back into your HTML. You could use HtmlHelper to do it, but it gets messy enough that the long-form conditional is much cleaner.
In the specific case of an attribute, however, there's a special feature of Razor, as of MVC4, that will help. If you specify an attribute using an expression that evaluates to null, MVC won't emit the attribute at all, so you can do:
<p style="#(this.Model.Sth ? "display:none;" : null)">Some Text</p>
(Note that null and string.Empty are different in this case: Razor will emit style="" if your expression evaluates to empty string.)

Replacing string with HTML control MVC View

<td style="font-size:1em;font-family:Verdana;color:darkkhaki">
#item.ObjQuestions.QuestionText.Replace("____", "<input type='Text'></input>")
</td>
I have seen a lot of similar questions, but I guess this is not a duplicate, because all the questions are how to add a control on serve-side. I want to replace a string that I get in my object.property in my cshtml View. I want to inject a textbox (or any html control) in place of some special characters.
The above code snippet simply replaces <input type='Text'></input> as text.
For e.g.
Acronym of CSS is ##
Here ## should be replaced by a textbox.
MVC will by default HTML encode all your strings in your view. You have tell it to output it as a raw HTML string instead by using Html.Raw:
#Html.Raw(item.ObjQuestions.QuestionText.Replace("____", "<input type='Text'></input>"))
Another option without having to use Html.Raw (security concern) is to split the string by the replacement word then combine them together with the HTML input:
#
{
string[] splits = item.ObjQuestions.QuestionText.Split(new string[] {"____"}, StringSplitOption.None);
}
#splits[0]
<input type='Text'></input>
#if (splits.Length > 1)
{
#: #splits[1]
}
This is for string with one placeholder only, if otherwise use a loop instead.

Resources