zoukankan      html  css  js  c++  java
  • 多个关键字高亮显示

    需求:搜索多个关键字  同时高亮显示

    实现:

    /// <summary>

    /// controller向view中传关键词

    /// </summary>
    /// <param name="search"></param>
    /// <returns></returns>

    public async Task<ActionResult> News_2052( string[] search = null)
    {
         search = search.Where(e => !string.IsNullOrEmpty(e)).ToArray();
         ViewBag.searchKeyWord = search;
         return View();
    }            

    @*视图 Index.cshtml 页面中*@


    @foreach (var item in Model as List<CnpiecNRP.Areas.Maintain.ViewModels.AllNewsViewModel>) { <tr> <td> @Html.HiddenFor(modelItem => item.Nid) @Html.HiddenFor(modelItem => item.Lcid) @if ((ViewBag.searchKeyWord as string[]).Length >= 1) { var title = item.SourceTitle; var keywords = ViewBag.searchKeyWord as string[]; for (int i = 0; i < (ViewBag.searchKeyWord as string[]).Length; i++) { var current = keywords[i]; title = new System.Text.RegularExpressions.Regex(current).Replace(title, "<span style='background:yellow;font-weight: bold;'>" + current + "</span>"); } @Html.Raw(title); } else { @Html.DisplayFor(modelItem => item.SourceTitle) } </td> <td> @Html.DisplayFor(modelItem => item.SourcePubDate, "MyDatetime") </td> <td> @Html.DisplayFor(modelItem => item.Enabled, "BoolTemplate") </td> <td> <a asp-action="Edit" asp-route-id="@item.Nid" asp-route-lcid="@item.Lcid">编辑</a> | <a asp-action="Delete" asp-route-id="@item.Nid" asp-route-lcid="@item.Lcid">删除</a> | <a class="ContentDetails">展开</a> </td> </tr>

     效果: 为保证可以看到搜索结果  所以只搜索了‘国际’和‘美国’

     

  • 相关阅读:
    shell编程介绍
    第一章作业
    jenkins介绍与操作
    gitlab介绍与操作
    github介绍与操作
    git介绍与操作
    zabbix监控tomcat与安全规范
    js-20170605-基本语法
    webstorm激活方法
    mac的一些基本设置(分享篇)
  • 原文地址:https://www.cnblogs.com/NICETOMEETYOUSEVEN/p/9339898.html
Copyright © 2011-2022 走看看