Hi guys,
I'm doing a basic Movie MVC application. I have a sorting that will display movies in the order of there
MovieId on the Home page, in the view, there is a link you can press to rearrange the movies alphabetically based on the Movie Name. However when I go to another Page and later return, the Sorting will always go back to being organised by MovieId, is there
anyway to be able to keep the sorting as alphabetical if I leave and return to the Home Page?
public class HomeController : Controller { private MovieDb db = new MovieDb(); public ActionResult Index(string sort, string Search_Data) { //Variable sort for sorting IQueryable<Movie> movie = db.Movies; ViewBag.SortingName = String.IsNullOrEmpty(sort) ? "Name_Description" : ""; //Search bar if (!String.IsNullOrEmpty(Search_Data)) { movie = movie.Where(s => s.MoviesName.Contains(Search_Data)); } //Search bar var albu = from alb in db.Movies select alb; { albu = albu.Where(alb => alb.MoviesName.ToUpper().Contains(Search_Data.ToUpper())); } //Sorting in switch switch (sort) { case "Name_Description": movie = movie.OrderBy(alb => alb.MoviesName); break; default: movie = movie.OrderBy(alb => alb.MovieID); break; } return View(movie.ToList()); } public ActionResult Details(int id = 0) { Movie m = db.Movies.Find(id); if (m == null) { return HttpNotFound(); } else { //HEY SHOW ME ACTORS m.Actors = (from e in db.Actors where e.MovieID.Equals(id) select e).ToList(); } //m.Actors.Count(); return View(m); } #region Create Movie public ActionResult Create() { return View(); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Create(Movie movie) { if (ModelState.IsValid) { db.Movies.Add(movie); db.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } #endregion #region Edit Movie public ActionResult Edit(int id) { Movie movie = db.Movies.Find(id); if (movie == null) { return HttpNotFound(); } return View(movie); } [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(Movie movie) { if (ModelState.IsValid) { db.Entry(movie).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(movie); } #endregion #region Delete Movie public ActionResult Delete(int id) { Movie movie = db.Movies.Find(id); return View(movie); } [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public ActionResult DeleteConfirmed(int id) { Movie movie = db.Movies.Find(id); db.Movies.Remove(movie); db.SaveChanges(); return RedirectToAction("Index"); } #endregion }
Here is the view
<h2>Movies</h2><p> @Html.ActionLink("All Actors", "Index", "Actor", null, new { @class = "btn btn-success" })</p><p> @Html.ActionLink("Create New", "Create", null, new { @class = "btn btn-primary" })</p> @using (Html.BeginForm("Index", "Home", FormMethod.Get)) {<p> Search Name: @Html.TextBox("Search_Data")<input type="submit" value="Filter" /></p> } @Html.ActionLink("Rearrange Alphabetically", "Index", new { sort = ViewBag.SortingName})<div class="table-responsive" ><table class="table table-striped"><tr class="info"><th> @Html.DisplayNameFor(model => model.MoviesName)</th><th> @Html.DisplayNameFor(model => model.Description)</th><th></th></tr> @foreach (var item in Model) { <tr><td><a href="@Url.Action("Details", null, new{id = item.MovieID})">@Html.DisplayFor(modelItem => item.MoviesName)</a></td><td> @Html.DisplayFor(modelItem => item.Description)</td><td><div class="form-group"><div class="btn-group" data-toggle="buttons"><label class="pdsa-radiobutton btn btn-warning active"><span class="glyphicon glyphicon-pencil"></span> @Html.ActionLink("Edit", "Edit", new { id = item.MovieID })</label></div></div></td><td><div class="form-group"><div class="btn-group" data-toggle="buttons"><label class="pdsa-radiobutton btn btn-danger active"><span class="glyphicon glyphicon-minus"></span> @Html.ActionLink("Delete", "Delete", new { id = item.MovieID })</label></div></div></td></tr> }</table></div> @section scripts { <script> $(function () {toastr.info("Click Movie for details") })</script> }
Thanks in advance
@{ ViewBag.Title = " Movie Details"; }<h2>Movie Details</h2><div class="btn-group" data-toggle="buttons"><label class="btn btn-info btn-group-sm "><span class="glyphicon glyphicon-arrow-left"></span> @Html.ActionLink("Return to Movie Menu", "Index")</label></div><div class="table-responsive"><table class="table table-striped"><tr><th><b>Movie Name:</b> @*@Html.DisplayFor(model => model.MoviesName)*@</th><th><b>Movie Descritpion:</b> @*@Html.DisplayFor(model => model.MoviesName)*@</th><th></th></tr><tr><td><i> @Html.DisplayFor(model => model.MoviesName)</i></td><td><i> @Html.DisplayFor(model => model.Description)</i></td><td>@*<button class="btn btn-xs btn-info"><a style="color: white;" href="@Url.Action("Edit", new { id = Model.MovieID })">Edit </a></button>*@</tr></table></div><p> @Html.ActionLink("Add Actor To Movie", "Create", "Actor", new { movieID = Model.MovieID }, new { @class = "btn btn-primary" })</p> @if (Model.Actors != null) {<h5>@Model.MoviesName has @Model.Actors.Count() Actors</h5><div class="table"><table class="table table-striped"><tr><th> Template: X is an actor in Y and played Z.</th><tr><td> @foreach (var mov in Model.Actors) {<p> @Html.DisplayFor(modelItem => mov.ActorsName) is an actor in @Html.DisplayFor(model => model.MoviesName) and played @Html.DisplayFor(modelItem => mov.ScreenName). @Html.ActionLink("Delete", "Delete", "Actor", new { id = mov.ActorID }, new { @class = "btn btn-danger btn-xs" }) @*<button class="btn btn-xs btn-info">@Html.ActionLink("Edit", "Edit", new { id = mov.ActorID })</button>*@ @*@Html.ActionLink("Edit", "Edit", "Actor", new { id = mov.ActorID }, new { @class = "btn btn-edit btn-xs" })*@ @*<button class="btn btn-xs btn-info"><a style="color: white;" href="@Url.Action("Edit", "Actors", new { id = mov.MovieID })"><span style="color:white;" class="glyphicon glyphicon-pencil"></span> Edit </a></button>*@ @*@Html.ActionLink("Edit Actors", "Edit", "Actor", null)*@ @*@Html.ActionLink("Edit", "Edit","Actor", null, new { @class = "btn btn-primary" })*@</p> }</td></tr></table></div> } else {<p class="alert alert-info">No actors found!!</p><p> @Html.ActionLink("Back to List", "Index")</p> }