asp.net-mvc – 有没有办法用asp.net mvc Razor ViewEngine制作
发布时间:2020-12-16 03:43:23 所属栏目:asp.Net 来源:网络整理
导读:我有一个类似于以下的Page.cshtml(不起作用): @{ Layout = "../Shared/Layouts/_Layout.cshtml"; var mycollection = (ViewBag.TheCollection as IQueryableMyCollectionType);}h2@ViewBag.Title/h2content here@if (mycollection != null mycollection.Cou
我有一个类似于以下的Page.cshtml(不起作用):
@{ Layout = "../Shared/Layouts/_Layout.cshtml"; var mycollection = (ViewBag.TheCollection as IQueryable<MyCollectionType>); } <h2>@ViewBag.Title</h2> content here @if (mycollection != null && mycollection.Count() > 0) { @section ContentRight { <h2> Stuff </h2> <ul class="stuff"> @foreach (MyCollectionType item in mycollection ) { <li class="stuff-item">@item.Name</li> } </ul> } } 正如我所说,这不起作用.如果集合中没有任何内容,我想不定义该部分.有没有办法让这样的工作?如果没有,我的其他选择是什么?我对这款Razor ViewEngine非常陌生. 编辑 在我的布局中我有: @if(IsSectionDefined("ContentRight")) { <div class="right"> RenderSection("ContentRight") </div> } 我不想要的是当该部分为空时输出的div. 解决方法
我最终做了一些有点hacky的事情来让它工作我需要它.
在我的页面上我有: @{ Layout = "../Shared/Layouts/_Layout.cshtml"; var mycollection = (ViewBag.TheCollection as IQueryable<MyCollectionType>); ViewBag.ShowContentRight = mycollection != null && mycollection.Count() > 0; } 然后在我的布局中我有: @if(IsSectionDefined("ContentRight") && (ViewBag.ShowContentRight == null ||ViewBag.ShowContentRight == true)) { <div class="right"> RenderSection("ContentRight") </div> } else if(IsSectionDefined("ContentRight")) { RenderSection("ContentRight") } 如果定义了部分,则必须进行渲染,但如果没有内容,我不想要< div> s 如果有更好的方式我想知道. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 一步步开发自己的博客 .NET版(4、文章发布功能)百度编辑
- asp.net-mvc – MVC ViewBag最佳实践
- asp-classic – ConnectionTimeout无效
- asp.net – 将自定义类添加到HttpContext
- asp.net-mvc – ASP MVC – 有默认内容类型的任何常量?
- asp.net-mvc – 启动了Just-In-Time调试器,没有必要的安全权
- asp.net – 使用jQuery以编程方式触发JavaScript中的事件
- asp.net-mvc-3 – 重定向与RedirectToRouteResult不工作从其
- asp.net-mvc – Orchard CMS中的SignalR聊天模块
- ASP.NET 2.0异步用户控件不起作用
推荐文章
站长推荐
- asp.net – 自定义主体在新请求时恢复为GenericP
- asp.net – HTTP 500内部错误 – IIS网站
- asp.net – ViewState vs cookies vs cashing vs
- 在基本控制器中覆盖ASP.NET MVC中的OnAuthorizat
- asp.net-mvc-3 – ASP.NET MVC 3:模型级客户端或
- asp.net-mvc – ASP.NET MVC Web API缓存控制头部
- Delegate如何进行类型转换?
- asp.net – 在IIS 7.5上挂在Session模块上的请求
- asp.net – 对于布尔查询字符串参数使用“true”
- asp.net – 让网站暂时无法使用的优雅方式?
热点阅读