我有一個基本的 asp.net mvc web 應用程序。它與我的數據庫相連,其中有許多記錄。我需要它在頁面頂部有一個過濾器選項和一個搜索名稱的搜索文本框。過濾器框應該是一個包含所有列名稱和選擇的選項的下拉框,與該選項相關的所有記錄都顯示在主索引頁面上。我試過這樣做,但它給了我錯誤。我附加了以下內容:DataFormsController(索引方法)、索引 html 頁面和 .models 頁面以及我在運行應用程序時遇到的錯誤。如果有人可以幫助我,我將不勝感激。謝謝?。?!RiskApplication.models 代碼: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; } }}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>
1 回答

慕田峪4524236
TA貢獻1875條經驗 獲得超5個贊
您正在將一個類型 RiskApplicationModel 從 Index 控制器傳遞給 Index 視圖但是 Index 視圖需要一個基于 cshtml @model IEnumerable 中第一行的 IEnumerable 類型的模型
因此,發送到視圖的內容與視圖期望的內容之間存在不匹配
這需要在控制器或視圖中修復??刂破餍枰l送視圖期望的類型,或者視圖應該將自身綁定到控制器發送的類型。
或者,控制器可以填充一個視圖模型,并且視圖可以像 MVVM 設計模式 (Model-View-ViewModel) 中那樣綁定到一個視圖模型
希望這可以幫助
- 1 回答
- 0 關注
- 202 瀏覽
添加回答
舉報
0/150
提交
取消