I have been reading a lot about this razor thing, and some examples present something like the following:
<input type="text" name="title" value="#Model.Title" />
I would like to do the same on my ASPX view. But no matter what I try, it just won't work. In other words, writing the following :
<input type="text" name="title" value="<%: Model.Title %>" />
Gave me a view with 2 text box, instead of what i'm trying to do. I don't want to use the HTML Helpers as they wont solve my problem. (which is to return a value from the view that is not included on my model).
If the value is in your view and not in your model then do not reference the model. Just reference the variable name you assigned to the value in your view.
<input type="text" name="title" value="#VariableName" />
Related
I just recently started using Thymeleaf through one of my projects. I have seen few examples where th:text=${example} is being used in some places th:value=${example}.
I have gone through the Thymeleaf documentation but couldn't find anything explicitly citing the difference, nor did any question on SO.
Any help would be really appreciated! Thanks.
th:value is modification of html attribute value.
For button, input and option elements, the value attribute specifies the initial value of the element
th:text is used for tag body modification.
div{background-color: lightblue; padding: 2px} // to highlight empty div
<!--th code: <div th:value="${value}"/></div> -->
<br/>Result th:value div: <div value="sometext"/></div>
<!--th code: <form><input th:value="${value}"/></form>-->
<br/>Result th:value form: <form><input value="sometext"></form>
<!--th code: <div th:text="${value}"></div>
Same as: <div>[[${value}]]</div> -->
<br/>Result th:text div: <div>sometext</div>
Here is docs of different Thymeleaf attributes features
Lets see an example:
<input type="radio" name="gender" value="male"> Male<br>
if we want to use thymeleaf in value portion of this input tag, then we will use,
<input type="radio" name="gender" th:value="${someValue}"> Male<br>
if we want to see the text (here Male) sent from the controller dynamically, then we use,
<input type="radio" name="gender" th:text="${someText}""> <br>
th:name => This would be the name of the value that you will be passing to another page (Exemplar scenario).
th:value => This would be the actual value that you would be passing. It could be obtained from a model or straight from the database explicitly.
<form th:action="#{confirm-pass-details.html}">
<button type="submit" th:name="event-id" th:value="${event.get().getEventid()}">Buy Passes</button>
</form>
I'm using areas to divide up my application and everything works as expected, including an EditorTemplate -- but only if the editor template in a certain location. My Project works when it is setup like this (note the location of MyEditor.cshtml):
\Areas\Area1\Controllers\TestController.cs
\Areas\Area1\Views\Test\Index.cshtml
\Areas\Area1\Views\Shared\EditorTemplates\MyEditor.cshtml
I am using an EditorTemplate (MyEditor.cshtml) inside MyViewIndex.cshtml and it works fine when I put it in the location listed above.
I can then move MyEditor.cshtml to the following location where MVC finds it, but doesn't render the Tag Helpers:
\Views\Shared\EditorTemplates\MyEditor.cshtml
So instead this Tag helper...
<input asp-for="Hello1" type="text" />
...rendering like this:
<input value="ABC" type="text" id="Hello1" name="Hello1" />
...it's output to the browser like it was never processed:
<input asp-for="Hello1" type="text" />
Inside the view Index.cshtml I have:
#Html.EditorFor(model => model.Hello1, "MyEditor")
Inside MyEditor.cshtml I have:
Hello<br/>
#DateTime.Now<br/>
<input asp-for="OneEditorProperty1" type="text" />
I know it's finding the editor template because it outputs "Hello" and I know it's processing it to some degree because I see the DateTime.Now value. For some reason the Input Tag Helper doesn't get processed.
What is missing from my question is where I have my #addTagHelper directives.
I had them here:
Areas\Area1\Views\_ViewImports.cshtml
But not here:
Views\_ViewImports.cshtml
I forget I even had to have this -- thanks to #dougbu over at https://github.com/aspnet for pointing this out.
I have this:
<input id="fileupload" type="file" name="files[]" data-url="rest/controller/upload" multiple>
and I need to make it work in a Thymeleaf template. I have the data-url part figured out but I keep getting an error on the word "multiple". This is needed to allow multiple selection in the file selection window.
I have looked everywhere and have not come across an answer.
EDIT:
If you are not familiar, here is the "multiple" attribute.
http://www.w3schools.com/tags/att_input_multiple.asp
The Standard Dialect of Thymeleaf includes attributes that allow you to set these attributes by evaluating a condition, so that if evaluated to true, the attribute will be set to its fixed value, and if evaluated to false, the attribute will not be set:
e.g for checkedattribute:
<input type="checkbox" name="active" th:checked="${user.active}" />
you have to use:
th:multiple
e.g
<input id="fileupload" type="file" name="files[]" th:multiple="{condition}">
see a tutorial here.
I have a section of Razor code in one of my views that isn't behaving the way I expect. The code in question contains multiple locations where I need to append the value of a variable from the Model onto an html attribute. The code below is a slightly simplified version of the markup on my page (I've removed some of the markup 'noise' that wasn't important to this example and just made the code harder to read).
There are three instances where I use #topic.StandardTopicId. The 1st and 2nd instances get replaced as I expected, but the 3rd instance name="IsCovered_#topic.StandardTopicId" renders without replacing the variable.
Code in question:
#foreach(var topic in Model.Group) {
<div id="frame-#topic.StandardTopicId" class="topic-frame">
<label>
<input type="radio" name="IsCovered_#(topic.StandardTopicId)" />
No
</label>
<label>
<input type="radio" name="IsCovered_#topic.StandardTopicId" />
Yes
</label>
</div>
}
Sample of the output from the above code block:
...
<div id="frame-42" class="topic-frame">
<label>
<input type="radio" name="IsCovered_42" />
No
</label>
<label>
<input type="radio" name="IsCovered_#topic.StandardTopicId" value="No" />
Yes
</label>
</div>
...
Can anyone explain why the last instance of the variable is not being replaced and is instead being rendered as is?
Razor will not parse the "#" in the middle of a word. The underscore is treated as part of a word as opposed to the dash. Use #() to place Razor code within words. #(topic.StandardTopicId)
Here's a reference to Razor syntax: C# Razor Syntax Quick Reference
Most likely, Razor considers IsCovered_#topic.StandardTopicId a valid e-mail address format, and therefore leaves it as-is.
I have a bunch of forms where its easier to type the entire html tags:
However, when I pass the Model to the View, it doesn't show the value. What am I missing here?
You will need to manually inject the values from ViewData. The helpers encapsulate this, but if you are writing straight HTML there's no way for ASP.NET MVC to know how to wire the values to the form elements.
<input type="text" id="Name" name="Name"
value="<%= ViewData.Model.Name %>" />