如何将参数传递给 RedirectToAction 到另一个 Action?

How to pass to RedirectToAction a parameter to another Action?

当用户完成调查并向数据库发送查询时,他应该return相同的视图,但每次都有不同的结果,具体取决于所选的选项。

来自用户方面:我有一个最喜欢的产品,但数据库中有成千上万的产品,调查是为了帮助我找到给定的产品,基于使用 entity framework 对数据库的查询。

当用户向controller发送数据时,controller在ViewBag中POST接收到数据,controller在switch中检查选中的选项:

控制器:

        [HttpGet]
        public IActionResult IndexExpert(string Id)
        {
            return View();
        }

        [HttpPost]
        public IActionResult IndexExpert(SelectModel select)
        {
            string age, modelife, money, typeproduct, colorproduct;

            colorproduct = ViewBag.colorProduct = select.colorproduct;
            typeproduct = ViewBag.typeProduct = select.typeproduct;
            modelife = ViewBag.ModeLife = select.ModeLife;
            age = ViewBag.Age = select.AgeProduct;
            money = ViewBag.Money = select.Money;

            int price = int.Parse(money);
            int old = int.Parse(age);
            List<Product> products = _context.Product.ToList();
            List<FirmProduct> firmProducts = _context.FirmProduct.ToList();
            List<ColorProduct> colorProducts = _context.ColorProduct.ToList();
            List<TypeProduct> typeProducts= _context.TypeHearAid.ToList();
            List<ModeLifeProduct> modeLifeProducts = _context.ModeLifeProduct.ToList();
            List<AgeProduct> ageProduct = _context.AgeProduct.ToList();


            switch (colorProducts)
            {
                case "red":
                    switch (typeProducts)
                    {
                        case "toy":
                            switch (modelife)
                            {
                                case "calm":
                                    switch (old)
                                    {
                                        case int m when (m >= 0 && m <= 25):
                                            switch (price)
                                            {
                                                case int n when (n <= 3500):
                                                    {
                                                        var query = (from s in products
                                                                     join hl in colorProducts on s.IdColorProduct equals hl.IdColorProduct into table0
                                                                     from hl in table0.ToList()
                                                                     join fh in firmProducts on s.IdNameFirm equals fh.IdNameFirm into table1
                                                                     from fh in table1.ToList()
                                                                     join th in typeProducts on s.IdTypeProducts equals th.IdTypeProducts into table2
                                                                     from th in table2.ToList()
                                                                     join ml in modeLifeProducts on s.IdModeLife equals ml.IdModeLife into table3
                                                                     from ml in table3.ToList()
                                                                     join ah in ageProduct on s.IdAgeCat equals ah.IdAgeCat into table4
                                                                     from ah in table4.ToList()
                                                                     where (hl.ColorProduct == "red" && th.TypeProduct == "toy" && ml.CatModeLife == "calm" && ah.CatAge == "junior" && s.Price <= 3500)
                                                                     select new ViewModel
                                                                     {
                                                                         Product = s,
                                                                         FirmProduct = fh,
                                                                         AgeProduct = ah,
                                                                         ColorProduct = hl,
                                                                         ModeLifeProduct = ml,
                                                                         TypeProduct = th
                                                                     });

                                                        return RedirectToAction(nameof(ExpertResult(), query);
                                                    }
                                              .
                                       ...
                        ....
             .....
.....(x30 SWITCH case)
}

.
.
.

        [HttpGet]
        public IActionResult ExpertResult()
        {
            return View();
        }

查看:

@model IEnumerable<ProductSystem.Areas.ProductSystem.Products.ViewModels.ViewModel>
@{
    Layout = ...;
}

<h1>Products</h1>
<div>
    <table class="table">
        <thead>
            <tr>
                <th>Model</th>
                <th>Producent</th>
                <th>Model</th>
                <th>Typ product</th>
                <th>Category age</th>
                <th>Color</th>
                <th>Mode life</th>
                <th>Price</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model)
            {
                <tr>
                    <td>
                        @Html.DisplayFor(modelItem => item.FirmProduct.NameFirm)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Product.Model)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.TypeProduct.TypeProduct)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.AgeProduct.CatAge)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ColorProduct.Color)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.ModeLifeProduct.CatModeLife)
                    </td>
                    <td>
                        @Html.DisplayFor(modelItem => item.Product.Price)
                    </td>
                </tr>
            }
        </tbody>
    </table>
</div>

NullReferenceException: Object reference not set to an instance of an object. AspNetCore.Areas_EarSystem_HearAids_Views_SystemExpert_ExpertResult.ExecuteAsync() in ExpertResult.cshtml + @foreach (var item in Model)

因为我可以将这些查询传递给一个视图,查询是可变的而视图是常量,所以视图结构是相同的,但其他结果是 returned.

理论上我只能对每个选项使用returning views(超过30个views),但我认为这太麻烦了,最好使用一个具有相关结果的通用视图。

我尝试了一个视图,一个查询,一个选项的一个案例,然后查询在 "ExpertResult" 操作中,我 return 查看(查询)和数据显示工作,但是我不想为给定的数据库查询创建每个单独的视图。

有什么想法吗?

我删除了:

 return RedirectToAction (nameof (ExpertResult (), query);

我替换了:

return View ("ExpertResult", query);

每当我必须使用 RedirectToAction 而不是 View 时,我只需创建 TempData 并在下一个方法中检索它。

public IActionResult IndexExpert(SelectModel select)
{
    ....
    TempData["Query"] = query;
    return RedirectToAction("ExpertResult");

}


    [HttpGet]
    public IActionResult ExpertResult()
    {
        var query = TempData["Query"] as ______;
        return View();
    }

它说的是 _____;你会把变量类型。因此,如果您有一个名为 "Query" 或 "DatabaseResponse" 或 "User" 的 class,您可以将

var query = TempData["Query"] as DatabaseResponse;

如果只是普通数据类型也可以这样做:

string query = TempData["Query"] as string;
string query = TempData["Query"].toString();

编辑:有时使用 RedirectToAction 比使用 returnView() 更好。我遇到过几次。如果您只想使用 return View()(这不会改变您的 url,但会改变用户看到的内容),那么只需

return View("ExpertResult", query);