0

I'm pretty much new in MVC and planing to do something, but I'm not sure is good practice or not. I would like to create ui controls as partial views. For example, I have autocomplete control, so I would like to have autocomplete partial views with scripts needed and everything, and also pass model through renderPartial?

So, I would like to hear your comments?

Grissom
  • 1,080
  • 8
  • 21
  • 2
    I personally use that a lot - and it works great. I prefer to get/post using ajax, but that's just preference. I wouldn't split a form in to several ui components, as this would make it quite difficult to post in the end, but things like "search", "change password", "user profile", "todays most clicked" always end up in partials. I mean, isn't that part of the whole idea :) – Mackan Apr 01 '15 at 13:59
  • 3
    On another note: I think this "question" is in danger of delition, as it's asking for opinions, not facts. I advise you to edit and add some actual factual question (also) – Mackan Apr 01 '15 at 14:02
  • You should never have scripts in partials (you could end up with duplicates). Partials are not the correct solution (you will not be able to use them for nested properties of complex objects or collections without also passing the property name prefix to the partial). Use `EditorTemplates` instead –  Apr 01 '15 at 21:32

1 Answers1

3

MVC offers a lot of options to separate view code that might be confusing in the begging. Main 4 of them are:

  • Partial Views
  • @helper Methods
  • HTML Helpers
  • Display/Editor Templates

I will not go in details here because there is a lot of info about them. For example @helper Methods vs HTML Helpers vs Partial Views. Or Editor/Display Templates vs Partial Views.

For complicated controls like autocomplete I would suggest to use Partial Views or HTML Helpers.

Community
  • 1
  • 1
Aleksandr Ivanov
  • 2,778
  • 5
  • 27
  • 35