ASP.NET MVC 动态模型未被识别
Posted
技术标签:
【中文标题】ASP.NET MVC 动态模型未被识别【英文标题】:ASP.NET MVC Dynamic Model not being recognized 【发布时间】:2021-10-02 10:11:24 【问题描述】:我正在尝试在 ASP.NET MVC 应用程序的 1 个视图中使用 2 个模型,并尝试使用此处描述的动态模型方法:https://www.c-sharpcorner.com/UploadFile/ff2f08/multiple-models-in-single-view-in-mvc/
这是我的控制器:
细节控制器
// GET: Statewides/Details/5
public ActionResult Details(int? id)
ViewBag.ID = id;
var today = DateTime.Today;
var todayAsString = today.ToString("MM/dd/yyyy");
ViewBag.Today = todayAsString;
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
Statewide statewide = db.Statewides.Find(id);
var document = from s in db.Documents
where s.Statewide_ID == id
select s;
dynamic mymodel = new ExpandoObject();
mymodel.Statewides = statewide;
mymodel.Documents = document;
if (statewide == null)
return HttpNotFound();
return View(mymodel);
这是我观点的一部分:
@model dynamic
@
ViewBag.Title = "Details";
<div class="row">
<div class="col-md-9 text-left">
<h5><strong>COUNTY:</strong> <span class="box-shadow px-3">@Model.Statewide.COUNTY</span></h5>
</div>
<div class="col-md-3 text-right district">
<h5><strong>District:</strong>@Model.Statewide.District</h5>
</div>
</div>
在该页面的底部,我需要访问 Documents 模型:
<table>
<tr>
<th>Doc Type</th>
<th>Order Date</th>
<th>Location</th>
</tr>
@foreach (Document document in Model.Documents)
<tr>
<td>@document.Doc_Type</td>
<td>@document.Order_Date</td>
<td>@document.Doc_Location</td>
</tr>
</table>
我在Document
上收到错误消息,提示“找不到类型或命名空间文档”。我不完全确定自己做错了什么,因为在我看来,我做了与指南相同的事情。
如果您需要,这里是完整视图,请注意我没有更改很多从旧模型中检索值的实例,这些实例仅在全州范围内。请忽略这些:
@model dynamic
<!-- CUSTOM CSS -->
<link href="~/Styles/Documents/DetailsStyle.css" type="text/css" rel="stylesheet">
<!-- GOOGLE ICONS -->
<link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
@
ViewBag.Title = "Details";
<div class="bigContainer">
<div class="container-fluid text-center topLabel">
<div class="borderline">
<h4 class="title"><strong>DETAIL RECORD - RP ROADWAY HISTORY FILE</strong></h4>
<div class="row">
<div class="col-md-1 printButton">
<input class="text-left" type="button" onclick="javascript:window.print()" value="Print" runat="server" />
</div>
<div class="col-md-11">
<p class="text-right">
<u>Current Date:</u>
<script>document.write(new Date().toLocaleDateString());</script>
</p>
</div>
</div>
</div>
<div class="row">
<div class="col-md-9 text-left">
<h5><strong>COUNTY:</strong> <span class="box-shadow px-3">@Model.Statewide.COUNTY</span></h5>
</div>
<div class="col-md-3 text-right district">
<h5><strong>District:</strong>@Model.Statewide.District</h5>
</div>
</div>
<div class="row">
<div class="col-md-4 text-left">
<h5><strong>Route No:</strong><span class="box-shadow px-4">@html.DisplayFor(model => model.RouteNo)</span></h5>
</div>
<div class="col-md-4">
<h5><strong>Sign System:</strong><span class="box-shadow px-4 sign">@Html.DisplayFor(model => model.SignSys)</span></h5>
</div>
<div class="col-md-4"></div>
</div>
<div class="row">
<div class="col-md-12 text-left">
<h5><strong>LocalName:</strong></h5>
</div>
</div>
<div class="row">
<div class="col-md-4 text-left">
<h5 class="box-shadow px-3 localName">@Html.DisplayFor(model => model.LocalName)</h5>
</div>
<div class="col-md-8"></div>
</div>
<div class="row suppMethodDuplicate">
<div class="col-md-2 text-right">
<h5><strong>Supp Des:</strong></h5>
</div>
<div class="col-md-2 text-left">
<h5>@Html.DisplayFor(model => model.SuppDes)</h5>
</div>
<div class="col-md-2 text-right">
<h5><strong>Method:</strong></h5>
</div>
<div class="col-md-2 text-left">
<h5>@Html.DisplayFor(model => model.Method)</h5>
</div>
<div class="col-md-1"></div>
<div class="col-md-2 text-left">
@Html.DisplayFor(model => model.Duplicate_OK)
<label class="form-check-label" for="duplicateCheck"><strong>Duplicate OK</strong></label>
</div>
</div>
<div class="row">
<div class="col-md-2 text-right">
<h5><strong>Date:</strong></h5>
</div>
<div class="col-md-2 text-left">
<h5>@Html.DisplayFor(model => model.ReservedDate)</h5>
</div>
<div class="col-md-2 text-right">
<h5><strong>Original Location:</strong></h5>
</div>
<div class="col-md-2 text-left">
<h5>@Html.DisplayFor(model => model.OriginalL)</h5>
</div>
</div>
<div class="row">
<div class="col-md-4"></div>
<div class="col-md-2 text-right">
<h5><strong>Original D:</strong></h5>
</div>
<div class="col-md-6 text-left">
<h6>@Html.DisplayFor(model => model.OriginalD)</h6>
</div>
</div>
<div class="borderline text-left">
<h5 class="mb-0"><strong>DOCUMENTS</strong></h5>
</div>
<table>
<tr>
<th>Doc Type</th>
<th>Order Date</th>
<th>Location</th>
</tr>
@foreach (Document document in Model.Documents)
<tr>
<td>@document.Doc_Type</td>
<td>@document.Order_Date</td>
<td>@document.Doc_Location</td>
</tr>
</table>
<div class="borderline text-left">
<h5 class="mb-0"><strong>PROJECTS</strong></h5>
</div>
<h6 class="text-left">@Html.DisplayFor(model => model.Projects)</h6>
<div class="borderline text-left">
<h5 class="mb-0"><strong>COMMENTS</strong></h5>
</div>
<h6 class="text-left">@Html.DisplayFor(model => model.Comments)</h6>
</div>
<p style="margin-top: 20px">
@Html.ActionLink("Back to List", "Index")
</p>
</div>
Serge 建议我使用视图模型,所以我尝试了这个:
视图模型
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Roadway_History.Models
public class ViewModel
public Statewide Statwide get; set;
public Document Document get; set;
动作
// GET: Statewides/Details/5
public ActionResult Details(int? id)
ViewBag.ID = id;
var today = DateTime.Today;
var todayAsString = today.ToString("MM/dd/yyyy");
ViewBag.Today = todayAsString;
if (id == null)
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
var model = new ViewModel
Statewide statewide = db.Statewides.Find(id);
var document = from s in db.Documents
where s.Statewide_ID == id
select s;
;
if (statewide == null)
return HttpNotFound();
return View(model);
但这也行不通。我得到错误
Statewide is a type which is not valid in the given context
和ViewModel does not contain a definition for statewide
我认为操作方法是错误的,但我不知道如何解决它。
另外,在视图中使用 @model ViewModel
会出现错误“找不到类型或命名空间 ViewModel”
【问题讨论】:
mvc 中的动态模型没有多大意义,因为 MVC 没有动态控件。使用动态数据和静态控件将如何受益? @Serge 那么你会推荐什么方法在同一个视图上使用 2 个模型?我需要同时访问 Statewides 和 Documents 模型。 【参考方案1】:如果您需要为一个视图使用两个模型,您可以创建一个包含这两个模型的视图模型
public class ViewModel
public Statewide Statwide get; set;
public List<Document> Documents get; set;
在视图中
@model ViewModel
动作
var model = new ViewModel
Statewide= ... your code
Document=... your code
;
......
return View(model);
更新
像这样修复你的视图模型
var statewide = db.Statewides.FirstOrDefault (i=> i.Id==id);
if (statewide == null)
return HttpNotFound();
var documents = db.Documents.Where(s=> s.Statewide_ID == id).ToList();
if (documents == null)
return HttpNotFound();
var model = new ViewModel
Statewide =statewide,
Documents = documents
;
return View(model);
但您也需要根据新的视图模型修复模型
【讨论】:
我已经更新了我的问题,以显示我在哪里尝试过您的建议。你能告诉我我在行动中做错了什么吗? 好的,当我运行它时,我收到此错误Unable to cast object of type 'System.Data.Entity.Infrastructure.DbQuery`1[Roadway_History.Models.Document]' to type 'Roadway_History.Models.Document'.
指向var model = new ViewModel
行
@GrillOwner69420 对不起。 c# 区分大小写 你的文档类名是什么?文件还是文件?强烈建议使用大写的类名。这就是我使用 Document 的原因。但如果它是这样的文档修复视图模型 - 公共文档 Documentget;设置;
@GrillOwner69420 如果您将我的代码用于视图模型,您应该不会有任何错误。你仔细检查我的代码了吗?
@GrillOwner69420 我修复了你的 EF 查询,它们有一些错误。请再试一次。以上是关于ASP.NET MVC 动态模型未被识别的主要内容,如果未能解决你的问题,请参考以下文章
ASP.NET MVC:动态添加部分视图并将它们反映在视图模型中
ASP.NET MVC 如何从嵌套对象的视图模型集合中动态创建表结构