Asp.net-mvc – User Control Equivalent in ASP.NET MVC 4

asp.net-mvc

I'm working on a new ASP.NET MVC 4 app. I'm really trying to do things the "MVC" way. In particular, I'm interested in using the Razor view engine. Please note that I come from a web forms background, so my questions may seem awkward.

I'm trying to learn if there is something equivalent to user controls in MVC? If so, what are they called? If not, what is the recommended way to creating a reusable component in MVC?

Best Answer

You're looking for @Html.Action and @Html.Partial.

They allow you to either render a partial view or a full blown controller action in a view. This is a common pattern of reuse in MVC land.

Occasionally you would want to make displayFor or editorFor templates, if a controller action is too heavy. The rule of thumb is if you need to do it multiple times on the page and it needs to be posted back in a form, think about doing it in a template.

Controls in asp.net cover a rather large swath which MVC granularizes a bit more.