Skip to main content

Posts

Showing posts from 2015

asp.net mvc different between ViewComponent Html.Partial Html.Action

asp.net mvc ViewComponent Html.Partial Html.Action的区别 mvc4及以前,通常使用Html.Partial("viewName"),Partial函数的返回值为MvcHtmlString,直接输出在Rozar模版中。 Html.RenderPartial("viewName")的返回值是void,输出内容流中。 查看mvc5的源代码发现Partial也是调用RenderPartial,然后输出到TextWriter来转换为字符串。 上面两个不用controller,不通过action。 Html.Action的返回值PartialView(),通过调用action来获取PartialView。 例如,通过ajax调用action来更新客户端Partial输出的内容。 mvc5中新增的ViewComponent,所有的都由ViewComponent继承而来。总体来说可认为是一个增强版Partial。 能有自己单独的代码文件,又不用的实现controller。 ref: http://www.cnblogs.com/gesenkof99/archive/2013/06/03/3115052.html http://stackoverflow.com/questions/29823507/why-should-we-use-mvc-6-feature-view-components-over-partial-view-what-is-the-d http://www.tugberkugurlu.com/archive/exciting-things-about-asp-net-vnext-series-mvc-view-components

_ViewStart.cshtml in asp.net mvc

从asp.net mvc 3开始Razor在返回 View() 的时候先执行"Views"根目录下的_ViewStart.cshtml,再执行对应的view文件 这就是为什么默认的模板套用能在这里找到 如果对应的controller目录下也存在"_ViewStart.cshtml"也存在话,那么一直顺序依次为: Views/_ViewStart.cshtml Views/[ControllerName]/_ViewStart.cshtml Views/[ControllerName]/[ViewName].cshtml 如果打算对某些的controller套用不同的模板,只需要将军的在对应的view目录下添加_ViewStart.cshtml并覆盖 Layout 就行。 参考阅读: Using ViewStart in ASP.NET MVC 3