I am looking for best practices for design razor view with MVC.
which would be better option:
HtmlHelper extension methods
@Html.TextBox("txtName")
or
write the html directly
<input type"text" id="txtName" name="txtName" />
I found 2 diferent links.
The first one http://blogs.msdn.com/b/aspnetue/archive/2010/09/17/second_2d00_post.aspx says DO use HTMLHelper extension methods.
and the second one http://codeclimber.net.nz/archive/2009/10/27/12-asp.net-mvc-best-practices.aspx says 10 – Write HTML each time you can
so i am a little cofused
Even the name
HtmlHelpershould already give you a hint whether you should use it or not. Do you want help? If not, just write html from the scratch. It does not really matter how the html was generated: from the scratch or using html helper. What matter is that it was generated with correct names of the inputs so that model binder can bind these inputs to the model.For example, suppose you have the following Model that will be passed to the view and that will be received on the POST:
In order to make sure that your inputs will be binded to the model you need three inputs on your page:
Having this html markup will assure proper model minding. However, you can achieve this markup by using HtmlHelpers, which is a lot easier:
This will not only give you proper
nameattributes on every input, but also assignidattributes accordingly so you don’t have to do that all by your self.It appears that the author from the second article suggests to never use HtmlHelpers for two reasons:
comfortable writing HTML” he means that developer should know
exactly what html markup is required for proper model binding.
markup will be generated by using HtmlHelpers or he just does not
know what html will be generated.
I disagree with his phrase: “HtmlHelpers whose only reason of living is hiding the HTML away”. I’d rather say “HtmlHelpers whose only reason of living is helping writing Html markup”
Summary:
HtmlHelpers help you write proper html markup, which is why I suggest you using it.