使用(上一个-下一个)在多个视图之间导航

时间:2019-03-26 18:19:55

标签: asp.net-mvc

我的网站上有全球30多个城市的信息。我正在将网站从Coldfusion转换为asp.net MVC。用户首先显示我站点上所有城市的列表,然后选择一个城市并显示信息。一旦他们显示了他们感兴趣的城市,我想允许他们显示列表中的下一个或上一个城市。列表可以3种方式之一进行排序。有什么建议吗?

示例: 清单: 北卡罗莱纳州的吴海滩- 南卡罗来纳州哥伦比亚市- 乔治亚州罗斯威尔- 霍克斯布拉夫,阿拉巴马州

用户选择罗斯威尔,通过ID(公共ActionResult CitiesDetails(int?id))进行访问 显示罗斯威尔页面后,如果用户选择下一步,则显示Hokes Bluff。从霍克斯布拉夫(Hokes Bluff),回到罗斯韦尔(Roswell),上一次再次展示哥伦比亚(Columbia)。

列表也可以按状态排序,因此 霍克斯布拉夫- 罗斯威尔- 吴海滩- 哥伦比亚

用户选择Roswell,下一个是Kure Beach,上一个是Hokes Bluff。

我可以想出一种方法来处理显示城市列表时构建的数组。问题是,现在要将数组从拳头视图传递到下一个视图,并在用户会话期间将其保留?手提包会为此工作吗?它会在多个视图之间保持不变吗?

--------- Controller ----------
--------- list all cities -------

namespace Lat34North.Controllers
{
    public class CitiesController : Controller
    {
        private Lat34NorthContext db = new Lat34NorthContext();

        // GET: Cities
        public ActionResult CitiesIndex(string sortOrder)
        {

            ViewBag.EastWestSortParm = string.IsNullOrEmpty(sortOrder)? "eastwest_desc" : "";
            ViewBag.CountrySortParm = sortOrder == "Country" ? "Country_Desc" : "Country";
            ViewBag.NameSortParm = sortOrder == "Name" ? "Name_Desc" : "Name";
            ViewBag.PopulationSortParm = sortOrder == "Population" ? "Population_Desc" : "Population";
            ViewBag.EastWestSortParm = sortOrder == "eastwest" ? "eastwest_Desc" : "eastwest";


        
            var cities = from s in db.Cities
                         where s.Display == true
                         select s;

            switch (sortOrder)
            {
                case "Country":
                    {
                        cities = cities.OrderBy(s => s.Country);
                        ViewBag.header = " ascending order by Country.";
                        break;
                    };

                case "Country_Desc":
                    {
                        cities = cities.OrderByDescending(s => s.Country);
                        ViewBag.header = " descending order by the Country.";
                        break;
                    };
               
               
                case "Name":
                    {
                        cities = cities.OrderBy(s => s.Name);
                        ViewBag.header = " ascending order by name of the city.";
                        break;
                    };

                case "Name_Desc":
                    {
                        cities = cities.OrderByDescending(s => s.Name);
                        ViewBag.header = " descending order by name of the city.";
                        break;
                    };
                case "eastwest_Desc":
                    {
                        cities = cities.OrderByDescending(s => s.EastWest);
                        ViewBag.header = " descending order from east (Bougtob, Algeria) to west (Fes, Morocco).";
                        break;
                    };
                case "eastwest":
                default:
                    {
                        cities = cities.OrderBy(s => s.EastWest);
                        ViewBag.header = " ascending order from west (Fes, Morocco) to east (Bougtob, Algeria).";
                        break;
                    };


             
            }



            //cities = cities.OrderBy(s => s.EastWest); 

            return View(cities.ToList());
        }



------------------- City Detail ------------
  public ActionResult USCitiesAL_Hokes_Bluff(int? id)
        {
            if (id == null)
            {
                return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
            }
            Cities cities = db.Cities.Find(id);
            if (cities == null)
            {
                return HttpNotFound();
            }
            return View(cities);
        }

1 个答案:

答案 0 :(得分:1)

我不知道您要如何精确地构造它,但基本设计可能是:

[HttpGet("/City/Details/{id:int}")]
public IActionResult CitiesDetails(int id)
{
    var list = GetMyCityList();

    var currentIndex = list.FindIndex(x => x == id);

    var model = new CityDetailsViewModel
    {
        CurrentCityId = id,
        PreviousCityId = list.ElementAtOrDefault(currentIndex - 1),
        NextCityId = list.ElementAtOrDefault(currentIndex + 1)
    };

    return View("CityDetails", model);
}

//replace this with your data source (database, file, or maybe you just want to keep it as a private list)
private List<int> GetMyCityList()
{
    //6, 3, and 5 are random numbers I chose to represent the city ids
    return new List<int> { 6, 3, 5 };
}

模型可以是:

public class CityDetailsViewModel
{
    public int? PreviousCityId { get; set; }
    public int CurrentCityId { get; set; }
    public int? NextCityId { get; set; }
}

视图可能是:

@model MyProject.Models.CityDetailsViewModel

<a asp-controller="CityController" asp-action="CitiesDetails" asp-route-id="@Model.PreviousCityId">Previous</a>
<a asp-controller="CityController" asp-action="CitiesDetails" asp-route-id="@Model.NextCityId">Next</a>

我没有处理任何边缘情况,例如如果当前城市是列表中的第一个或最后一个城市,该怎么办...等等。但这应该给您一个入门的思路。