Bu...">

What is the correct way to "#include file" in MVC?

I would like to do something like this:

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

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

+5
source share
3 answers
<%: Html.Partial("~/Views/foo/box.ascx") %>

or

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

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

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

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

<%: 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)
+14
source

You must make a partial view.

+2
source

Html.RenderPartial('~/Views/Login/Box.ascx');

RenderPartial allows you to display part of the page using the same context. If you want to render using a new context, use

Html.RenderAction("Box","Login"); //Box - Action, Login - Controller
+2
source

All Articles