我试图在使用Razor
的View页面中实现这个算法,但是它没有显示预期的结果,也没有得到任何编译错误。有什么建议吗? 编辑:我道歉我不是很清楚,我承认。我的问题是,我不明白为什么ViewBag.NbrePages
等于0.但是,数据库已填充。如何有效地使用剃刀
Action();
[HttpGet]
public ActionResult Rechercher(string rech, string type, int num = 1)
{
int nbLignesDepassees = 10 * (num - 1);
ViewBag.Recherche = Server.HtmlEncode(rech);
ViewBag.Type = Server.HtmlEncode(type);
ViewBag.NumPgeCourrante = num;
if (type == "nomAppMetier")
{
var appsMetiers = _db.AppsMetiers
.Where(x => SqlFunctions.PatIndex("%" + rech + "%", x.nomApplication) > 0)
.OrderBy(x => x.nomApplication)
.Skip(nbLignesDepassees)
.Take(10);
ViewBag.NbrePages = (int)(appsMetiers.Count()/10) ;
return View("RechercheAppsMetiers",appsMetiers);
}
if (type == "nomPoste")
{
var postes = _db.Postes
.Where(x => SqlFunctions.PatIndex("%" + rech + "%", x.nomPoste) > 0)
.OrderBy(x => x.nomPoste)
.Skip(nbLignesDepassees)
.Take(10);
ViewBag.NbrePages = (int)(postes.Count()/10);
return View("RecherchePostes", postes);
}
return HttpNotFound();
}
查看();
<ul>
@{
for (int i = 0; i < ViewBag.NbrePages; i++)
{
if(i==1 || i==2 || i==3){
<li class="disabled"><a href="#">&maquo;</a></li>
}else{
<li><a href="#">«</a></li>
}
if (i == ViewBag.NumPgeCourrante)
{
<li class="active"><a href="#">@i <span class="sr-only">(current)</span></a></li>
}
else
{
<li><a href="#">@i </a></li>
}
if(i==ViewBag.NbrePages || i==ViewBag.NbrePages-1 || i==ViewBag.NbrePages-2){
<li class="disabled"><a href="#">»</a></li>
}else{
<li><a href="#">»</a></li>
}
}
}
</ul>
非常感谢!
你期待什么结果,并显示什么?此外,在这个视图中还有很多逻辑。通常这应该放在控制器中,以便创建一个模型,并将其传递给强类型视图。这使得代码更容易理解。 –
没有显示,我期望显示一个Integer元素列表。哇,那么有什么建议如何使这个代码工作进行调用控制器+视图,而不是只有一个视图? – user3212730
如果没有显示,可能是'ViewBag.NbrePges'为零。在这种情况下'
'会被渲染。 –