如何有效地使用剃刀

时间:2014-01-30 07:41:26

标签: c# asp.net-mvc asp.net-mvc-4 razor

我正在尝试使用Razor在View页面中实现此算法,但是,它不显示预期的结果,并且我没有收到任何编译错误。有什么建议吗? 编辑:我道歉我不太清楚,我承认。我的问题是我不明白为什么ViewBag.NbrePages等于0.但是,数据库已经填满了。

动作();

[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="#">&laquo;</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="#">&raquo;</a></li>
            }else{
                <li><a href="#">&raquo;</a></li>
            }
        }
}
</ul>

非常感谢!

2 个答案:

答案 0 :(得分:1)

不要在视图中使用如此多的逻辑,请考虑以下内容:

模特

public class PagesModel
{
    public int NumberOfPages { get; set; }

    public int CurrentPage { get; set; }
}

中的辅助方法
public static class Helpers
{
    public static bool GetClassNames(int page, int totalPages, int currentPage)
    {
        var classNames = new List<string>();

        var isWithinFirstOrLastThree = page <= 2 || page >= (totalPages - 2);

        if (isWithinFirstOrLastThree)
        {
            classNames.Add("disabled");
        }
        if (page == currentPage)
        {
            classNames.Add("active");
        }

        return string.Join(" ", classNames.ToArray());
    }
}

然后您的观点可以像

一样简单
@model PagesModel

@for (int i = 0; i < Model.NumberOfPages; i++)
{
    <li class="@Helpers.GetClassNames(i, Model.NumberOfPages, Model.CurrentPage)">
        <a href="#">&maquo;</a>
        @i
    </li>
}

这与你想要达到的目标不完全一致,但我希望它仍然有用。

答案 1 :(得分:0)

由于NbrePages并使用整数除法,

Take(10)将为0或1(如果您有超过10条记录):

 ViewBag.NbrePages = (int)(appsMetiers.Count() / 10) ;

所以你最有可能在appsMetiers中获得少于10个项目。

建议根据CSHTM中变量的原始拼写错误来改进来源:

使用好名字或强类型模型有助于避免原始帖子中NbrePges条件中的for拼写:

for (int i = 0; i < ViewBag.NbrePages; i++)

CSHTML文件直到运行时访问才编译,因此没有编译错误。由于ViewBag允许使用任何属性,因此您也不会收到任何智能警告。

而不是ViewBag考虑一些强类型模型或者至少将强类型对象用于分页ViewBag

 class PagingState 
 { 
      public int NumberOfPages { get;set;}
      public int CurrentPage { get;set;}
 } 

并在视野中:

 var pageingState = (PagingState)(ViewBag.Paging);
 for(int I = 0; i < pageingState.NumberOfPages; i++)...