Reputation: 2509
Being a newbie to MVC and Razor I am wondering what is different in generating html controls using standard HTML tags as compared to using Razor synetx.
Example for HTML tags:
<input type="hidden" id="hidden1"/>
<input id="txtSearch" type="text" class="content-box span3" name="Keywords" placeholder="Keywords">
Example for Razor tags:
@(Html.Hidden("hfType"))
<input type="text" id="txt" class="txt1"/>
Is there a difference or these are just 2 ways of doing same things. In which cases I should prefer which way. I highly appreciate your guidance on it.
Upvotes: 0
Views: 46
Reputation: 191027
They are called HTML helpers. They just provide convenience methods. It especially helps with the naming of nested properties. They also can provide access the metadata for the model like for validation and labels.
Another benefit would be that the attribute strings would be escaped properly. It eliminates this bad pattern.
<a href="@someValue">Test</a>
Upvotes: 1
Reputation: 16378
You're more talking about HTML helpers than Razor. An appreciation for HTML helpers comes with time.
Writing and rewriting HTML by hand is time consuming and it does not follow the DRY principle. You end up having very similar code all over the place and if you need to change it then it can become a nightmare in a larger application.
Related: I asked Are HTML helpers worth using with complex markup?
I use HTML helpers more than ever now.
Upvotes: 0