Why do we use HTML helper in ASP.NET MVC?

前端 未结 2 1650
我寻月下人不归
我寻月下人不归 2021-01-04 01:24

Are there any good thing, best practice or profit we have after using the HTML helper in an ASP.NET MVC project?

When I am trying to use them I found that I lose the

相关标签:
2条回答
  • 2021-01-04 01:46

    You use HTML helpers to encapsulate some small HTML fragments which are repeated all over your pages. And to avoid writing those HTML snippets all over again you use helpers.

    They are very useful, especially when dealing with things like URLs because instead of hardcoding your links helpers take advantage of routing the definition on your server and by simply changing those routes the whole site URLs' change without ever touching any single HTML page.

    Another scenario where HTML helpers are useful is for generating form input fields. In this case they automatically could handle values when posting back and show associated validation messages. Can you imagine the spaghetti code you would have to write in your views if there weren't HTML helpers?

    0 讨论(0)
  • 2021-01-04 02:00

    The biggest advantage I find is with the editor and display templates.

    If your editor for a field is more than just a simple input box, you can put that into a template and replace the several tags with a call to

    <%:Html.EditorFor(m=>m.Property)%>

    This means that your page is a lot easier to edit as you aren't wading through a lot of fluff HTML to find what you want.

    0 讨论(0)
提交回复
热议问题