I'm working on an ASP.net MVC Razor view that someone else wrote. I see that it contains the following:
<span>
#:
</span>
I know that the # symbol allows me to insert code into a view, but what does #: stand for?
In MVC, # is the respective char that allows you to use razor inside HTML (inside a .cshtml) which in runtime (or precompiled) will be converted to c#.
With # you may write C# within HTML and with #: you may write HTML within C#.
Example:
#foreach (TestClass item in Model)
{
#:#item.Code - #item.Name
}
Without the #: it wouldn't be possible to do this, since all the chars after the first # will be considered as C#.
This way you are saying that you are getting the two variables from item and placing the char - between them and the result is a content block (or html/text)
Related
I am working with a multi layer web application that uses various web technologies. It interacts with a WebAPI to get the data via Knockout JS.
I will try to narrow the scope of my question to this:
I have a span tag
<span id="title" data-bind="text: FullTitle"></span>
I would like to get the content of that span and assign it to a Razor variable.
I have tried:
#
{
var Test = <span id="title" data-bind="text: FullTitle"></span>
}
But this doesn't render the knockout code and it just assigns the full span code to that variable.
I just want to get the full title when knockout pulls it into the Test variable.
Note: the span works outside the razor block and displays the full title on the cshtml view.
Any ideas?
Thanks in advance.
I've got the following cshtml code in my razor view.
<div class="col left-col pull-left">
#for (int i = 0; i < #Model.NewsResults.Count; i = i + 2)
{
NewsResult article1 = #Model.NewsResults[i];
<div>#i - #article1.Title #Html.Partial("NewsItemPartial", article1)</div>
}
</div>
The #i ... seems to tell razor to ouput the data. If I leave the out there, nothing renders.
Please explain how I can do what I have below but without having to use the (before the #i).
You can tell razor to output HTML by putting the text tag instead of div:
<text>#i - #article1.Title #Html.Partial("NewsItemPartial", article1)</text>
Razor is getting confused because it thinks the - is a subtraction operator, and it's trying to subtract Title from i. you could use Mark's method, or you could make it even more localized by putting the <text> block around the -.
#i <text>-</text> #article1.Title #Html.Partial("NewsItemPartial", article1)
However, it looks to me like you're trying to implement a template output, which MVC already provides a mechanism for, called DisplayTemplates. You should really use those instead of Partials.
As has already been mentioned, Razor is considering the - to be a subtraction operator because of its position right after the #i, which is converting it into a C# block.
You can use <text></text> to convert any block (single line or multiline) into a text literal in Razor.
#i <text>-</text> #article1.Title #Html.Partial("NewsItemPartial", article1)
Or better yet, for a single line (or single character as your example), you can alternatively use the #: operator.
#i #:- #article1.Title #Html.Partial("NewsItemPartial", article1)
Reference: http://haacked.com/archive/2011/01/06/razor-syntax-quick-reference.aspx/
<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.
I have HTML Pages in my ASP.net MVC project. How can i render these pages with dynamic values.
Index.html
< input type="text" value=<%=Dynamic Value Here%> />
You can't since HTML files will not be parsed by the ASP.NET MVC view engine and it won't understand the server side syntax you will need to insert dynamic values. In order to do this you will need to translate your HTML files into aspx files to use the webforms view engine syntax of <%: %> or cshtml files to use the razor syntax of #. Now you will be able to insert dynamic values since the view engine will insert those values into the HTML that is returned to the browser to be rendered.
This might be really simple question and I though the <text> tags were the answer but they do not seem to be working on my part.
I simply want to display #azamsharp in my asp.net mvc application.
<text> #azamsharp </text>
gives me the following error:
The name 'azamsharp' does not exist in the current context
##azamsharp
Razor see the # and thinks you're escaping into code, so it looks for a variable azamsharp. ## tells razor you want to print an # symbol, not escape into code.
Try this instead:
##azamsharp
That should work