你如何在 MVC 中保持剃刀页面排序?

How do you keep a razor page sort in MVC?

我正在做一个基本的 Movie MVC 应用程序。我有一个排序,可以按那里的顺序显示电影 主页上的 MovieId,在视图中,有一个 link 您可以按以根据电影名称按字母顺序重新排列电影。但是,当我转到另一个页面并稍后 return 时,排序将始终返回到按 MovieId 组织的方式,如果我离开并 return 到主页?

    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
}

这是主页

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

提前感谢您提供的任何帮助

您可以将已排序的字段存储在您的会话中,以了解用户是否已排序。

由此改变

    //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());

为此:

     if (Session["sort"] == null)
        {
            switch (sort)
            {
                case "Name_Description":
                    movie = movie.OrderBy(alb => alb.MoviesName);
                    Session["sort"] = sort;
                    break;
                default:
                    movie = movie.OrderBy(alb => alb.MovieID);
                    break;
            }
        }
        else
        {
            movie = movie.OrderBy(alb => alb.MoviesName);
        }