This example for you to share the MVC4 book management system of the production of tutorials for your reference, the specific contents are as follows
First Project Structure chart:
the relevant code for the model layer is as follows:
The Book.cs code is as follows:
public class book
{
[Key]
[databasegenerated (databasegeneratedoption.identity)] public
Guid Id {get ; Set
[MaxLength]
[Display (Name = "title")] public
string Title {get; set;}
[MaxLength (5000)]
[Display (Name = "Preface")]
public string Foreword {get; set;}
[Display (Name = "total pages")]
public int Pages {get; set;}
[Display (Name = "author")]
public string Author {get; set;}
}
public class Appcontext:dbcontext
{public
appcontext ()
{
} public
dbset<book> Books {get; set;}
Related to ViewModels:
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;}
}
Next on the HomeController.cs and BooksController.cs code:
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 = ten) {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); }
}
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); The book book = db.
Books.find (ID);
if (book = = null) {return httpnotfound ();
Return to View (book);
Public ActionResult Create () {return View (); [HttpPost] [Validateantiforgerytoken] public ActionResult Create ([Bind (include= Id,title,foreword,pages,autho R ")] book book) {if (modelstate.isvalid) {book.
Id = Guid.NewGuid (); Db.
Books.add (book); Db.
SaveChanges ();
Add book elasticsearch.elasticsearch.client.index<book>;
Return redirecttoaction ("Index");
Return to View (book); Public ActionResult Edit (Guid? id) {if (id = = NULL) {return new Httpstatuscoderesult Httpstatuscode.bad RequeST); The book book = db.
Books.find (ID);
if (book = = null) {return httpnotfound ();
Return to 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 to View (book); Public ActionResult Delete (Guid? id) {if (id = = NULL) {return new Httpstatuscoderesult httpstatuscode.b
Adrequest); The book book = db.
Books.find (ID);
if (book = = null) {return httpnotfound ();
Return to 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. 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);
}
}
Elasticsearch Auxiliary class:
First, Elasticsearch.cs.
public class Elasticsearch
{
private static elasticclient _client;
public static elasticclient Client
{
get
{
if (_client = = null)
{
//Connection configuration
var setting = New ConnectionSettings (Elasticsearchconfiguration.connection,elasticsearchconfiguration.defaultindex);
_client = new Elasticclient (setting);
}
return _client;
}
}
ElasticsearchConfiguration.cs class
public static class Elasticsearchconfiguration
{public
static string Host {get {return ' http://localhost ';} Public
static Long Port {get {9200}
}} The public static Uri Connection the
{get
{new Uri (string). Format ("{0}:{1}", Host, Port));
the public
static string Defaultindex
{get
{return "library"
}}}}
SearchService.cs code:
public class Searchservice {public double Minscore {get {0.0005;}}
Highlight prefix public string Prehighlighttag {get {@ @ ' <strong> ';}
}//Highlight tag suffix public string Posthighlighttag {get {@ @ ' </strong> ';}
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 to new Tuple<ienumerable<ihit<book>> long> (result. Hits,result.
Elapsedmilliseconds); }//Lookup phrase suggested 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 = Elasticsearch.Elasticsearch.Client.Search
<Book> (S => s.allindices ()); return result.
Hits;
}
}
views View
Books folder:
Index.cshtml:
@model ienumerable<library.web.models.book> @{viewbag.title = "Index";
Layout = "~/views/shared/_layout.cshtml";
}
Edit.cshtml:
@model Library.Web.Models.Book @{viewbag.title = "Edit";
Layout = "~/views/shared/_layout.cshtml"; }
Details.cshtml:
@model Library.Web.Models.Book @{viewbag.title = "Details";
Layout = "~/views/shared/_layout.cshtml"; }
Delete.cshtml:
@model Library.Web.Models.Book @{viewbag.title = "Delete";
Layout = "~/views/shared/_layout.cshtml"; }
Create.cshtml:
@model Library.Web.Models.Book @{viewbag.title = "Create";
Layout = "~/views/shared/_layout.cshtml"; }
Home->index.cshtml
@model Library.Web.ViewModels.SearchViewModel @{viewbag.title = "Elasticsearch"; <div class= "Jumbotron" >
_layout.cshtml
The result is as shown in figure:
List page
To create a page:
Search Results page:
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.