本文實例為大家分享了MVC4圖書管理系統的製作教程,供大家參考,具體內容如下
首先專案結構圖:
Model層的相關程式碼如下:
Book.cs代碼如下:
Model層的相關程式碼如下:
ViewModels的相關:public class Book { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } [MaxLength(500)] [Display(Name = "标题")] public string Title { get; set; } [MaxLength(5000)] [Display(Name = "前言")] public string Foreword { get; set; } [Display(Name = "总页数")] public int Pages { get; set; } [Display(Name = "作者")] public string Author { get; set; } }接下來就HomeController.cs和BooksController.cs的程式碼:
public class AppContext:DbContext { public AppContext() { } public DbSet<Book> Books { get; set; } }
public class SearchViewModel { public string Query { get; set; } public IEnumerable<IHit<Book>> Results { get; set; } public IDictionary<string, Suggest[]> Suggestions { get; set; } public long Elapsed { get; set; } }
Elasticsearch輔助類別:首先是Elasticsearch.cs
public class HomeController : Controller { private SearchService _searchService; public HomeController() { _searchService = new SearchService(); } public ActionResult Index() { return View(); } public ActionResult Search(string query, int page = 0, int pageSize = 10) { var result = _searchService.Find(query, page, pageSize); var suggestion = _searchService.FindPhraseSuggestion(query, 0, 3); var viewModel = new SearchViewModel { Query = query, Results = result.Item1,Elapsed = result.Item2, Suggestions = suggestion }; return View("Index", viewModel); } }Elasticsearchsearch.
首先是Elasticsearch.cs
public class BooksController : Controller { private AppContext db = new AppContext(); public ActionResult Index() { return View(db.Books.ToList()); } public ActionResult Details(Guid? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Book book = db.Books.Find(id); if (book == null) { return HttpNotFound(); } return View(book); } public ActionResult Create() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create([Bind(Include="Id,Title,Foreword,Pages,Author")] Book book) { if (ModelState.IsValid) { book.Id = Guid.NewGuid(); db.Books.Add(book); db.SaveChanges(); //添加书 Elasticsearch.Elasticsearch.Client.Index<Book>(book); return RedirectToAction("Index"); } return View(book); } public ActionResult Edit(Guid? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Book book = db.Books.Find(id); if (book == null) { return HttpNotFound(); } return View(book); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit([Bind(Include="Id,Title,Foreword,Pages,Author")] Book book) { if (ModelState.IsValid) { db.Entry(book).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(book); } public ActionResult Delete(Guid? id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } Book book = db.Books.Find(id); if (book == null) { return HttpNotFound(); } return View(book); } [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(Guid id) { Book book = db.Books.Find(id); db.Books.Remove(book); db.SaveChanges(); return RedirectToAction("Index"); } public JsonResult Reindex() { foreach (var book in db.Books) { //Indexing book Elasticsearch.Elasticsearch.Client.Index<Book>(book); } return Json("OK",JsonRequestBehavior.AllowGet); } protected override void Dispose(bool disposing) { if (disposing) { db.Dispose(); } base.Dispose(disposing); } }
public class Elasticsearch { private static ElasticClient _client; public static ElasticClient Client { get { if (_client == null) { //连接配置 var setting = new ConnectionSettings(ElasticsearchConfiguration.Connection,ElasticsearchConfiguration.DefaultIndex); _client = new ElasticClient(setting); } return _client; } } }
Views視圖
Books資料夾下:
Index.cshtml:
public static class ElasticsearchConfiguration { public static string Host { get { return "http://localhost"; } } public static long Port { get { return 9200; } } public static Uri Connection { get { return new Uri(string.Format("{0}:{1}", Host, Port)); } } public static string DefaultIndex { get { return "library"; } } }
Edit.cshtml:
public class SearchService { public double MinScore { get {return 0.0005; }} //高亮标记前缀 public string PreHighlightTag { get { return @"<strong>"; } } //高亮标记后缀 public string PostHighlightTag { get { return @"</strong>"; } } public Tuple< IEnumerable<IHit<Book>>,long> Find(string query, int page = 0, int pageSize = 10) { var result = Elasticsearch.Elasticsearch.Client.Search<Book>(s => s .From(page * pageSize) .Size(pageSize) .MinScore(MinScore) .Highlight(h => h .PreTags(PreHighlightTag) .PostTags(PostHighlightTag) .OnFields( f => f.OnField(b => b.Foreword), f => f.OnField(b => b.Title) )) .Query(q => q.QueryString(qs => qs.Query(query).UseDisMax()))); return new Tuple<IEnumerable<IHit<Book>>, long>(result.Hits,result.ElapsedMilliseconds); } //查找短语建议 public IDictionary<string, Suggest[]> FindPhraseSuggestion(string phrase, int page = 0, int pageSize = 5) { var result = Elasticsearch.Elasticsearch.Client.Search<Book>(s => s .From(page*pageSize) .Size(pageSize) .SuggestPhrase("did-you-mean", ps => ps .Text(phrase) .OnField(f => f.Foreword)) .Query(q => q.MatchAll())); return result.Suggest; } public IEnumerable<IHit<Book>> FindAll() { var result = Elasticsearch.Elasticsearch.Client.Search<Book>(s => s.AllIndices()); return result.Hits; } }
Details.cshtml:
@model IEnumerable<Library.Web.Models.Book> @{ ViewBag.Title = "Index"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2 id="Index">Index</h2> <p> @Html.ActionLink("创建新书", "Create") </p> <table class="table"> <tr> <th> @Html.DisplayNameFor(model => model.Title) </th> <th> @Html.DisplayNameFor(model => model.Foreword) </th> <th> @Html.DisplayNameFor(model => model.Pages) </th> <th> @Html.DisplayNameFor(model => model.Author) </th> <th></th> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.Title) </td> <td> @Html.DisplayFor(modelItem => item.Foreword) </td> <td> @Html.DisplayFor(modelItem => item.Pages) </td> <td> @Html.DisplayFor(modelItem => item.Author) </td> <td> @Html.ActionLink("编辑", "Edit", new { id=item.Id }) | @Html.ActionLink("详细", "Details", new { id=item.Id }) | @Html.ActionLink("删除", "Delete", new { id=item.Id }) </td> </tr> } </table>cs
.cshtml. .cshtml
@model Library.Web.Models.Book @{ ViewBag.Title = "Edit"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2 id="Edit">Edit</h2> @using (Html.BeginForm()) { @Html.AntiForgeryToken() <div class="form-horizontal"> <h4 id="Book">Book</h4> <hr /> @Html.ValidationSummary(true) @Html.HiddenFor(model => model.Id) <div class="form-group"> @Html.LabelFor(model => model.Title, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Title) @Html.ValidationMessageFor(model => model.Title) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Foreword, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.TextAreaFor(model => model.Foreword) @Html.ValidationMessageFor(model => model.Foreword) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Pages, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Pages) @Html.ValidationMessageFor(model => model.Pages) </div> </div> <div class="form-group"> @Html.LabelFor(model => model.Author, new { @class = "control-label col-md-2" }) <div class="col-md-10"> @Html.EditorFor(model => model.Author) @Html.ValidationMessageFor(model => model.Author) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-10"> <input type="submit" value="Save" class="btn btn-default" /> </div> </div> </div> } <div> @Html.ActionLink("返回列表", "Index") </div> @section Scripts { @Scripts.Render("~/bundles/jqueryval") }
_Layout.cshtml
@model Library.Web.Models.Book @{ ViewBag.Title = "Details"; Layout = "~/Views/Shared/_Layout.cshtml"; } <h2 id="Details">Details</h2> <div> <h4 id="Book">Book</h4> <hr /> <dl class="dl-horizontal"> <dt> @Html.DisplayNameFor(model => model.Title) </dt> <dd> @Html.DisplayFor(model => model.Title) </dd> <dt> @Html.DisplayNameFor(model => model.Foreword) </dt> <dd> @Html.DisplayFor(model => model.Foreword) </dd> <dt> @Html.DisplayNameFor(model => model.Pages) </dt> <dd> @Html.DisplayFor(model => model.Pages) </dd> <dt> @Html.DisplayNameFor(model => model.Author) </dt> <dd> @Html.DisplayFor(model => model.Author) </dd> </dl> </div> <p> @Html.ActionLink("编辑", "Edit", new { id = Model.Id }) | @Html.ActionLink("返回列表", "Index") </p>
結果如圖:
列表頁
本文解釋瞭如何使用printf中的\ n逃脫序列在C中創建新線字符並列出函數。 它詳細介紹了功能並提供了代碼示例,以說明其用於輸出中的線路斷裂。

本文探討了C中的無指針啟用的挑戰。它認為問題本身不是零,而是濫用。 本文詳細介紹了預防退出的最佳實踐,包括提出前檢查,指針pitiberi

本文指導初學者選擇C編譯器。 它認為,海灣合作委員會由於其易用性,廣泛的可用性和廣泛的資源,最適合初學者。 但是,它也比較了海灣室,Clang,MSVC和TCC,突出了它們的差異

本文強調了NULL在現代C編程中的持續重要性。 儘管取得了進步,但NULL對於明確的指針管理仍然至關重要,從而通過標記沒有有效的內存地址來防止細分故障。 最好的prac

本文回顧了初學者的在線C編譯器,重點是易用性和調試功能。 在線GDB和REPL。 其他選項,例如Programiz和Compil

本文討論了C IDE中的有效代碼複製。 它強調,複製是IDE功能,而不是編譯器功能,並且詳細提高了效率的策略,包括使用IDE選擇工具,代碼折疊,搜索/替換,Templa

本文比較在線C編程平台,突出了諸如調試工具,IDE功能,標準合規性和內存/執行限制等功能的差異。 它認為“最佳”平台取決於用戶需求

該教程通過在Windows,MacOS和Linux上安裝C編譯器來指導用戶。 它詳細介紹了流行編譯器(Mingw,Visual Studio,Xcode,GCC)的安裝,解釋了環境變量配置,並提供故障排除步驟


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

Atom編輯器mac版下載
最受歡迎的的開源編輯器

Dreamweaver Mac版
視覺化網頁開發工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。