MVC检查Layout中的表是否为空
本文关键字:是否 检查 Layout MVC | 更新日期: 2023-09-27 18:25:04
我想检查布局页面中的表是否为空,如果为空,我想删除导航栏中指向新闻索引的链接。
这就是我目前所掌握的:
型号:
public class News
{
[Key]
public int NID { get; set; }
[UIHint("tinymce_jquery_full"), AllowHtml]
public string Newstext { get; set; }
[DisplayFormat(DataFormatString = "{0:yyyy/MM/dd}",
ApplyFormatInEditMode = true)]
public DateTime Datum { get; set; }
public bool Empty
{
get
{
return (NID == 0 && string.IsNullOrWhiteSpace(Newstext));
}
}
}
_Layout.cshtml:
@if (Model.Empty)
{
<li><a href="http://localhost:10075/#nyheter">Nyheter</a></li>
}
else {
}
现在的问题是它无法读取Model.Empty
是什么,如果我把@model SeikoConsulting.Models.News
放在_Layout.cshtml
的顶部,我会得到这个错误:
传递到字典中的模型项的类型为"System.Collections.Generic.List`1[ImageSlider.Models.Gallery]",但此字典需要类型为"SeikoConsulting.Models.News"的模型项。
您不应该将该逻辑放在布局中。而是在布局中定义一个可以插入内容的部分。因此,在您的布局中,使用RenderSection
方法执行类似操作:
@RenderSection("EmptyTableSection", false)
然后在您的视图中,您可以将内容插入到布局中,如下所示:
@section EmptyTableSection {
<li><a href="http://localhost:10075/#nyheter">Nyheter</a></li>
}