I'm new to Razor but experienced in classic html and C#.
For the beginning I have done the razor tutorial from Microsoft. After doing it I wanted to play with the code and so I got my question.
I want to make the view-code more dynamic by creating the html by reflection (this view-code I mean). In future I want to generate whole html-forms by a model without the need to write html.
There are property blocks like this two examples (Title and Release Date):
<div class="form-group">
<label asp-for="Movie.Title" class="control-label"></label>
<input asp-for="Movie.Title" class="form-control" />
<span asp-validation-for="Movie.Title" class="text-danger"></span>
</div>
<div class="form-group">
<label asp-for="Movie.ReleaseDate" class="control-label"></label>
<input asp-for="Movie.ReleaseDate" class="form-control" />
<span asp-validation-for="Movie.ReleaseDate" class="text-danger"></span>
</div>
It is easy in C# to read that properties by reflection and create the html. But when I create the html I could not use the special "asp-for" and "asp-validation-for" attributes.
For testing if the server-side rendering supports the special "asp"-attributes I created the hardcoded (experimental) code below:
@Html.Raw("<div class=\"form-group\">")
@Html.Raw("<label asp-for=\"Movie.Title\" class=\"control-label\"></label>")
@Html.Raw("<input asp-for=\"Movie.Title\" class=\"form-control\" />")
@Html.Raw("<span asp-validation-for=\"Movie.Title\" class=\"text-danger\"></span>")
@Html.Raw("</div>")
As I expected is the validation for "Title" no longer working.
My questions are:
- How generate by reflection asp-forms (or at least is there a Html.Raw pendant for such a purpose?)
- How does the "asp"-attributes work in the background (is the compiler doing the magic or the runtime?)
Thanks for your effort!
Aucun commentaire:
Enregistrer un commentaire