什么是MVC中的“#包括文件'的正确方法? [英] What is the right way to '#include file' in MVC?

查看:106
本文介绍了什么是MVC中的“#包括文件'的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我愿做这样的事情:

 <! - #include文件=../东西/富/ box.aspx - >

但在一个ASP.Net MVC应用程序这样做只是觉得不妥。是否有一个ASP.Net MVC项目实现同样的事情的一个更好的办法?


解决方案

 <%:Html.Partial(〜/查看/富/ box.ascx)%>

 <%Html.RenderPartial(〜/查看/富/ box.ascx); %GT;

或其中最好的使用编辑模板(如果这部分包含了编辑视图模型属性输入):

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

或显示模板(如果这部分只包含视图模型属性显示):

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

和他们的剃刀等价

  @ Html.Partial(〜/查看/富/ box.ascx)
@ {Html.RenderPartial(〜/查看/富/ box.ascx);}
@ Html.EditorFor(X => x.MyModelProperty)
@ Html.DisplayFor(X => x.MyModelProperty)

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 ?

解决方案

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

这篇关于什么是MVC中的“#包括文件'的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆