Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the right way to '#include file' in MVC?

Tags:

asp.net-mvc

I would like to do something like this:

<!--#include file="../stuff/foo/box.aspx"-->

But doing this in an ASP.Net MVC application it just feels wrong. Is there a better way of achieving the same thing in a ASP.Net MVC project ?

like image 550
7wp Avatar asked Feb 16 '11 20:02

7wp


2 Answers

<%: Html.Partial("~/Views/foo/box.ascx") %>

or:

<% Html.RenderPartial("~/Views/foo/box.ascx"); %>

or the best of them all use an editor template (if this partial contains inputs for editing the view model property):

<%: Html.EditorFor(x => x.MyModelProperty) %>

or a display template (if this partial contains only display of view model property):

<%: Html.DisplayFor(x => x.MyModelProperty) %>

and their Razor equivalence

@Html.Partial("~/Views/foo/box.ascx")
@{Html.RenderPartial("~/Views/foo/box.ascx");}
@Html.EditorFor(x => x.MyModelProperty)
@Html.DisplayFor(x => x.MyModelProperty)
like image 94
Darin Dimitrov Avatar answered Oct 05 '22 23:10

Darin Dimitrov


You should make a partial view.

like image 30
SLaks Avatar answered Oct 05 '22 23:10

SLaks