开发者

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

I would like to do somet开发者_如何学编程hing 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 ?


<%: 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)


You should make a partial view.


You can use

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

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

Html.RenderAction("Box","Login"); //Box - Action, Login - Controller
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜