多个关键字高亮显示

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

实现:

/// <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>

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

 

猜你喜欢

转载自www.cnblogs.com/NICETOMEETYOUSEVEN/p/9339898.html
今日推荐