首页 > 解决方案 > 搜索重定向并显示到当前页面,然后在选择项目时直接到另一个页面

问题描述

我正在创建一个汽车租赁网站。我会展示所有可用的车辆,并在一辆被租用/预订后将它们从展示中移除。

在我的可用车辆的显示页面上,我希望使用下拉列表来搜索类别,但是,我希望执行搜索并将用户返回到此页面,仅包含经过优化的搜索车辆。此后,一旦用户选择了他想要预订的车辆,他就必须被引导到下一页。

我的车辆 ActionResult:

public ActionResult Vehicles()
    {
        var e = db.Vehicles.Where(x => x.availability == true).ToList();
        return View(e);
    }

我的车辆 ActionResult,发布:

[HttpPost]
    public ActionResult Vehicles(string locationUp, string vehicleID)
    {
        Session["V_LOC"] = locationUp;
        Session["V_ID"] = vehicleID;

        return RedirectToAction("Vehicle_Step_1", "Home");
    }

就目前而言,这将用户引导至预订车辆的第二步,我如何适应搜索以使用户保持在同一页面上并优化搜索,一旦用户选择车辆,然后将他们引导至第二步步?

我以前没有这样做过,所以我对如何继续进行有点困惑

标签: c#asp.net-mvc

解决方案


您可以定义一个模型,该模型定义了用户过滤器:

public class Filter 
{
    public string MaxPrice { get; set; }
    public string Make { get; set; }
}

在您的搜索页面中,您需要将此过滤器发布到控制器以优化搜索,因此添加另一个用户可以发布到的操作方法:

[HttpPost] // <-- note that this one is HttpPost
public ActionResult Vehicles(Filter userFilter)
{
    // refine the search and send the user back to Vehicles view
    var e = db.Vehicles.Where(x => x.availability == true && 
                              x.Price <= userFilter.MaxPrice &&
                              string.Equal(x.Model, userFilter.Make).ToList();
    return View(e);
}

这是您最简单的选择……替代方法是使用 Ajax 进行搜索,并使用 Ajax 和 JavaScript 更新同一页面上的搜索结果(这肯定会更复杂)。


推荐阅读