【发布时间】: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