【问题标题】:How to pass to RedirectToAction a parameter to another Action?如何将参数传递给 RedirectToAction 到另一个 Action?
【发布时间】:2020-03-30 15:05:31
【问题描述】:

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

从用户的角度来看:我有一个喜欢的产品,但数据库中有数千种产品,调查是为了帮助我找到给定的产品,基于使用实体框架对数据库的查询。

当用户向控制器发送数据时,控制器在ViewBag中的POST中接收数据,控制器检查开关中选择的选项:

控制器:

        [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:对象引用未设置为 目的。 AspNetCore.Areas_EarSystem_HearAids_Views_SystemExpert_ExpertResult.ExecuteAsync() 在 ExpertResult.cshtml + @foreach(模型中的变量项)

由于我可以将这些查询传递给一个视图,查询是可变的,视图是常量,所以视图结构相同,但返回其他结果。

理论上我可以只为其他选项使用返回视图(超过 30 个视图),但我认为这太麻烦了,最好使用一个具有相关结果的通用视图。

我尝试使用一个查询的一个视图和一个选定选项的情况,然后查询位于“ExpertResult”操作内,我返回视图(查询)并且数据显示有效,但我不想创建每个给定数据库查询的各个视图。

有什么想法吗?

【问题讨论】:

  • 那么真正的问题是什么?
  • @ChrisPratt 谢谢你的回答,我忘了写我的错误,我编辑了这篇文章。
  • 基于异常,Model 似乎为空。您是返回类似View(query) 还是只返回View()?如果是后者,那是你的问题。您需要以Model 的形式向视图发送一些内容。
  • 我该如何转发到 View?....
  • 通过将其传递给View() 调用,正如我所说,即return View(myModel);

标签: asp.net-mvc asp.net-core expert-system


【解决方案1】:

我删除了:

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

我换了:

return View ("ExpertResult", query);

【讨论】:

    【解决方案2】:

    每当我必须使用 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”的类,您可以放置​​

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

    如果它只是一个普通的数据类型,你也可以这样做:

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

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

    return View("ExpertResult", query);
    

    【讨论】:

    • 我明白了,想象一下我不得不坐在上面几个小时才意识到只需要更改一行代码。啊,这个编程:P
    • @DominikN 我们都去过那里!
    【解决方案3】:

    你应该创建一个 ViewModel

    public class VM_Query
    {
        public int Id { get; set; }
        public string Product { get; set; }
        public string FirmProduct { get; set; }
        public string AgeProduct { get; set; }
        public string ColorProduct { get; set; }
        public string ModeLifeProduct { get; set; }
        public string TypeProduct { get; set; }
    }
    

    并将您的数据设置为此 ViewModel ForExample

     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
                                });
    
    VM_Query q=new VM_Query();
    
    q.Product=query.Product;
    q.FirmProduct=query.FirmProduct;
    .
    .
    .
    
    return RedirectToAction(nameof(ExpertResult(), q);
    

    并将您的操作更新为该操作

    public IActionResult ExpertResult(VM_Query query)
    {
        return View();
    }
    

    并且在 Action IndexExpert 中使用此代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-22
      • 2011-10-20
      • 2012-09-25
      • 2020-10-03
      • 2011-08-10
      相关资源
      最近更新 更多