ASP.NET MVC#Web应用程序-从数据库筛选/搜索获取错误
我有一个基本的asp.net mvc web应用程序。它与我的数据库连接,我的数据库有很多记录。我需要它在页面顶部有一个过滤器选项和一个搜索文本框来搜索名字。过滤器框应该是一个下拉框,包含所有列名,并且选择了哪个选项,与该选项相关的所有记录都显示在主索引页上。我曾试过这样做,但它给了我错误。我附加了以下内容:DataFormsController(index方法)、index html页面和.models页面,以及运行应用程序时遇到的错误 如果有人能帮我,我会非常感激 谢谢 RiskApplication.models代码:ASP.NET MVC#Web应用程序-从数据库筛选/搜索获取错误,c#,html,asp.net,asp.net-mvc,C#,Html,Asp.net,Asp.net Mvc,我有一个基本的asp.net mvc web应用程序。它与我的数据库连接,我的数据库有很多记录。我需要它在页面顶部有一个过滤器选项和一个搜索文本框来搜索名字。过滤器框应该是一个下拉框,包含所有列名,并且选择了哪个选项,与该选项相关的所有记录都显示在主索引页上。我曾试过这样做,但它给了我错误。我附加了以下内容:DataFormsController(index方法)、index html页面和.models页面,以及运行应用程序时遇到的错误 如果有人能帮我,我会非常感激 谢谢 RiskApplic
using System.Collections.Generic;
//using Microsoft.AspNetCore.Mvc.Rendering;
using System.Web.Mvc;
namespace RiskApplication.Models
{
public class RiskApplicationModel
{
public List<DataForm> dataForms;
public SelectList owners;
public string department { get; set; }
}
}
使用System.Collections.Generic;
//使用Microsoft.AspNetCore.Mvc.Rendering;
使用System.Web.Mvc;
名称空间和应用程序模型
{
公共类风险应用程序模型
{
公共列表数据表;
公共选择名单所有者;
公共字符串部门{get;set;}
}
}
index.cshtml:
@model IEnumerable<RiskApplication.Models.DataForm>
@{
ViewBag.Title = "Index";
}
<h2>RiskPrescreen Application 2018</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<form asp-controller="DataForms" asp-action="Index" method="get">
<p>
<select asp-for="department" asp-items="Model.owners">
<option value="">All</option>
</select>
Application Name: <input type="text" name="SearchString">
<input type="submit" value="Search" />
</p>
</form>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.Name)
</th>
@*<th>
@Html.DisplayNameFor(model => model.Application_Exec)
</th>
<th>
@Html.DisplayNameFor(model => model.SME)
</th>
<th>
@Html.DisplayNameFor(model => model.Project_Directory_Location)
</th>
<th>
@Html.DisplayNameFor(model => model.IRDR_name)
</th>
<th>
@Html.DisplayNameFor(model => model.Description)
</th>
<th>
@Html.DisplayNameFor(model => model.Risk_Statement)
</th>*@
<th>
@Html.DisplayNameFor(model => model.InherentRisk)
</th>
@*<th>
@Html.DisplayNameFor(model => model.AppOwner_DD.AppOwnerDes)
</th>
<th>*@
@* @Html.DisplayNameFor(model => model.C1_DD.C1)
</th>
<th>
@Html.DisplayNameFor(model => model.C2_DD.C2)
</th>
<th>
@Html.DisplayNameFor(model => model.C3_DD.C3)
</th>
<th>
@Html.DisplayNameFor(model => model.C4_DD.C4_des)
</th>
<th>
@Html.DisplayNameFor(model => model.C5_DD.C5)
</th>
<th>
@Html.DisplayNameFor(model => model.C6_DD.C6)
</th>
<th>
@Html.DisplayNameFor(model => model.SDLC_Stage_DD.SDLC_Stage)
</th>*@
<th></th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.Name)
</td>
@*<td>
@Html.DisplayFor(modelItem => item.Application_Exec)
</td>
<td>
@Html.DisplayFor(modelItem => item.SME)
</td>
<td>
@Html.DisplayFor(modelItem => item.Project_Directory_Location)
</td>
<td>
@Html.DisplayFor(modelItem => item.IRDR_name)
</td>
<td>
@Html.DisplayFor(modelItem => item.Description)
</td>
<td>
@Html.DisplayFor(modelItem => item.Risk_Statement)
</td>*@
<td>
@Html.DisplayFor(modelItem => item.InherentRisk)
</td>
@*<td>
@Html.DisplayFor(modelItem => item.AppOwner_DD.AppOwnerDes)
</td>
@model IEnumerable
@{
ViewBag.Title=“Index”;
}
2018年风险预筛选申请
@ActionLink(“新建”、“创建”)
全部的
应用程序名称:
@DisplayNameFor(model=>model.Name)
@*
@DisplayNameFor(model=>model.Application\u Exec)
@DisplayNameFor(model=>model.SME)
@DisplayNameFor(model=>model.Project\u目录\u位置)
@DisplayNameFor(model=>model.IRDR\u name)
@DisplayNameFor(model=>model.Description)
@DisplayNameFor(model=>model.Risk_语句)
*@
@DisplayNameFor(model=>model.inherentisk)
@*
@DisplayNameFor(model=>model.AppOwner\u DD.AppOwnerDes)
*@
@*@Html.DisplayNameFor(model=>model.C1\u DD.C1)
@DisplayNameFor(model=>model.C2\u DD.C2)
@DisplayNameFor(model=>model.C3\u DD.C3)
@DisplayNameFor(model=>model.C4\u DD.C4\u des)
@DisplayNameFor(model=>model.C5\u DD.C5)
@DisplayNameFor(model=>model.C6\u DD.C6)
@DisplayNameFor(model=>model.SDLC\u Stage\u DD.SDLC\u Stage)
*@
@foreach(模型中的var项目){
@DisplayFor(modelItem=>item.Name)
@*
@DisplayFor(modelItem=>item.Application\u Exec)
@DisplayFor(modelItem=>item.SME)
@DisplayFor(modelItem=>item.Project\u目录\u位置)
@DisplayFor(modelItem=>item.IRDR\u名称)
@DisplayFor(modelItem=>item.Description)
@DisplayFor(modelItem=>item.Risk_语句)
*@
@DisplayFor(modelItem=>item.Inherentisk)
@*
@DisplayFor(modelItem=>item.AppOwner\u DD.AppOwnerDes)
DataFormsController.cs:
public async Task<ActionResult> Index(string department, string searchString)
{
// Use LINQ to get list of genres.
IQueryable<string> genreQuery = from m in db.AppOwner_DD
orderby m.AppOwnerDes
select m.AppOwnerDes;
var dataForms = from m in db.DataForms
select m;
if (!String.IsNullOrEmpty(searchString))
{
dataForms = dataForms.Where(s => s.Name.Contains(searchString));
}
if (!String.IsNullOrEmpty(department))
{
dataForms = dataForms.Where(x => x.AppOwner_DD.AppOwnerDes == department);
}
var movieGenreVM = new RiskApplicationModel();
movieGenreVM.owners = new SelectList(await genreQuery.Distinct().ToListAsync());
movieGenreVM.dataForms = await dataForms.ToListAsync();
return View(movieGenreVM);
}
公共异步任务
您正在从索引控制器向索引视图传递类型RiskApplicationModel
但是,索引视图需要基于cshtml@model IEnumerable中的第一行的IEnumerable类型的模型
因此,发送到视图的内容与视图期望的内容不匹配
这需要在控制器或视图中修复。
控制器需要发送视图期望的类型,或者视图应该将自身绑定到控制器正在发送的类型
或者,控制器可以填充一个viewmodel,视图可以绑定到一个viewmodel,就像MVVM设计模式(Model-view-viewmodel)一样
希望这有帮助您得到的错误是什么?在问题本身而不是附件中共享代码如何?@ChetanRanpariya刚刚共享了代码。让我知道您是否能够看到它也附加了我在图像中得到的错误视图需要类型为
IEnumerable
的模型,但您发送的是的单个对象RiskApplicationModel
到视图。这就是为什么会出现此错误。@ChetanRanpariya-Hmmm,我到底应该在哪里插入它?有点困惑