@model Library.Web.Models.Book @{ ViewBag.Title = "Create"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2>创建</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div> <h4>Book</h4> <hr /> @Html.ValidationSummary(true) <div> @Html.LabelFor(model => model.Title, new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title) </div> </div> <div> @Html.LabelFor(model => model.Foreword, new { @class = "control-label col-md-2" }) <div> @Html.TextAreaFor(model => model.Foreword) @Html.ValidationMessageFor(model => model.Foreword) </div> </div> <div> @Html.LabelFor(model => model.Pages, new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.Pages) @Html.ValidationMessageFor(model => model.Pages) </div> </div> <div> @Html.LabelFor(model => model.Author, new { @class = "control-label col-md-2" }) <div> @Html.EditorFor(model => model.Author) @Html.ValidationMessageFor(model => model.Author) </div> </div> <div> <div> <input type="submit" value="创建" /> </div> </div> </div> } <div> @Html.ActionLink("回到列表", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
Home->Index.cshtml
@model Library.Web.ViewModels.SearchViewModel @{ ViewBag.Title = "Elasticsearch"; } <div> <h1>Elasticsearch入门</h1> <p>安装和配置群集</p> <ol> <li> <a href="http://www.oracle.com/technetwork/java/ javase/downloads/index.html">安装Java</a> </li> <li> <a href="http://www.elasticsearch.org/ download/">安装Elasticsearch</a> </li> <li>运行Elasticsearch</li> <li><a href="https://www.jb51.net/Books/Create">增加一些书籍</a></li> </ol> </div> @if (Model == null) { return; } <div> @if (Model.Suggestions.Any(x => x.Key == "did-you-mean")) { <span>你的意思是: </span> foreach (var suggestions in Model.Suggestions["did-you-mean"]) { var count = 0; foreach (var suggestion in suggestions.Options) { <a href="/Home/Search?query=@suggestion.Text"><strong>@suggestion.Text </strong> </a> count++; } if (count == 0) { <span>没有建议!</span> } } } </div> <h3><strong>Results for:</strong> @Model.Query</h3> @if (Model != null) { <table> <thead> <tr><th>文档的分数(排名相关度)</th><th>Title</th><th>Content</th><th>Author</th></tr> </thead> <tbody> @foreach (var result in Model.Results) { <tr> <td>@result.Score</td> <td> <a href="https://www.jb51.net/Books/Details/@result.Id"> @if (result.Highlights != null && result.Highlights.Any(x => x.Key == "title")) { var hl = result.Highlights.FirstOrDefault(x => x.Key == "title"); foreach (var h in hl.Value.Highlights) { WriteLiteral(h); } } else { WriteLiteral(result.Source.Title); } </a> </td> <td> @if (result.Highlights != null && result.Highlights.Any(x => x.Key == "foreword")) { var hl = result.Highlights.FirstOrDefault(x => x.Key == "foreword"); foreach (var h in hl.Value.Highlights) { WriteLiteral(h + "..."); } } </td> <td>@result.Source.Author</td> </tr> } @if (!Model.Results.Any()) { <tr> <td colspan="4">没有结果发现:(</td> </tr> } </tbody> </table> <h4><span>@Model.Results.Count()</span>搜索结果用了 @Model.Elapsed 毫秒</h4> }
_Layout.cshtml
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta content="width=device-width, initial-scale=1.0"> <title>@ViewBag.Title</title> @Styles.Render("~/Content/css") @Scripts.Render("~/bundles/modernizr") </head> <body> <div> <div> <div> <button type="button" data-toggle="collapse" data-target=".navbar-collapse"> <span></span> <span></span> <span></span> </button> @Html.ActionLink("Elasticsearch MVC示例", "Index", "Home", null, new { @class = "navbar-brand" }) </div> <div> <ul> <li>@Html.ActionLink("Home", "Index", "Home")</li> <li>@Html.ActionLink("Books", "Index", "Books")</li> </ul> @using (Html.BeginForm("Search", "Home", FormMethod.Get,new {@class = "navbar-form navbar-left"})) { <div> <input type="text" placeholder="搜索" /> </div> <button type="submit">提交</button> } </div> </div> </div> <div> @RenderBody() <hr /> <footer> <p>© @DateTime.Now.Year - Elasticsearch, Nest, ASP.NET 应用</p> </footer> </div> @Scripts.Render("~/bundles/jquery") @Scripts.Render("~/bundles/bootstrap") @RenderSection("scripts", required: false) </body> </html>
结果如图:
列表页
创建页:
搜索结果页: