我正在尝试迭代存储在ASP.NET MVC4项目中控制器的字段中的IList。我试图在我的一个视图中执行此操作,以便能够生成与通过搜索查询收集的某些信息相对应的HTML元素。代码可以在下面看到。
public class HomeController : Controller
{
public static int MovieHits;
public static int PeopleHits;
public static IList<Model.MovieDto> Movies;
public static IList<Model.PersonDto> People;
public ActionResult SearchResults(string input)
{
ViewBag.Title = "Search";
ViewBag.Message = "Your search returned the following match(es).";
var results = Model.ModelController.UniversalSearch(input);
Movies = results.MovieListForUser;
People = results.PersonListForUser;
MovieHits = Movies.Count;
PeopleHits = People.Count;
return View();
}
<div id="movieResults">
@foreach (var movie in HomeController.Movies)
{
<span class="movieResult">@movie.Title</span><br/>
}
</div>
问题是我在浏览器中尝试生成HTML时出现编译错误。错误信息可以在下面看到,但要注意;它已被谷歌从丹麦语翻译成英语,但从它的外观来看,它看起来很容易理解= P
描述:在编译处理此请求所需的资源期间发生错误。阅读有关错误的以下详细信息,并对源代码进行必要的更改。 kompileringsfunktion出错:CS0012:类型'Model.MovieDto'在未引用的程序集中定义。添加对程序集“Model,Version = 0.0.0.0,Culture = neutral,PublicKeyToken = null”的引用。来源错误:
Line 20: <td>
Line 21: <div id="movieResults">
Line 22: @ foreach (var movie inHomeController.Movies)
Line 23: {
Line 24: <span class="movieResult"> @ movie.Title < / span> <br/>
详细输出:
C: \ Program Files (x86) \ IIS Express> "C: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ csc.exe" / t: library / utf8output / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 2296c65a \ 68000ed7_61f2ce01 \ WebGrease.dll "/ R:" C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 27f75d7a \ 0852d4d4_61f2ce01 \ EntityFramework.dll "/ R:" C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 5496f055 \ 42f708d7_61f2ce01 \ Antlr3.Runtime.dll " / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 0ee8ac00 \ fe53add5_61f2ce01 \ System.Web.Http.dll" / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ App_global.asax.of7azw0j.dll" / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 998cd3c3 \ f48cb7d5_61f2ce01 \ System.Web.Http.OData.dll" / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ c455315f \ d0673bd4_61f2ce01 \ DotNetOpenAuth.Core.dll" / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET / R: "C: \ Windows \ Microsoft.NET \ Framework \ v4.0.30319 \ Mscorlib.dll" / R: "C: \ Windows \ Microsoft.Net \ assembly \ GAC_MSIL \ System \ v4.0_4.0.0.0__b77a5c561934e089 \ System.dll" / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP . NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 07cc8738 \ 3289a6d4_61f2ce01 \ DotNetOpenAuth.OpenId.dll "/ R:" C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 33bc8f69 \ 9b5d66bd_4df8ce01 \ nunit.framework.dll "/ R:" C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 93c58068 \ 93b2a5d5_61f2ce01 \ System.Net.Http.Formatting.dll " / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 2f2e565f \ 5ed196d6_61f2ce01 \ Newtonsoft.Json.dll" / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 3ca568a6 \ 56e0bed5_61f2ce01 \ System.Web.Http.WebHost.dll" / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 2d57ee1b \ 804ef2d5_61f2ce01 \ Microsoft.Data.Edm.dll" / R: "C : \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 6d3f6044 \ 23229fd5_61f2ce01 \ System.Web.Optimization.dll" / R: "C : \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 895f125b \ 5996f043_56f8ce01 \ Webclient.dll " / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ ebbe30cd \ 47e537d6_61f2ce01 \ Microsoft.Data.OData.dll" / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 51baa225 \ 4f2d9ad4_61f2ce01 \ DotNetOpenAuth.OAuth.dll" / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ cd36e272 \ f779c0d6_61f2ce01 \ System.Spatial.dll" / R: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 96b97b00 \ 3ebd2ed4_61f2ce01 \ DotNetOpenAuth.AspNet.dll " / Out: "C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ App_Web_searchresults.cshtml.a8d08dba.6gvjhlym.dll" / D: DEBUG / debug + / optimize-/ w : 4 / nowarn: 1659; 1699, 1701, 612, 618 / warnaserror-"C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ App_Web_searchresults.cshtml.a8d08dba.6gvjhlym .0. cs "" C: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ App_Web_searchresults.cshtml.a8d08dba.6gvjhlym.1.cs "
Microsoft (R) Visual C # 2010 Compiler version 4.0.30319.17929
for Microsoft (R). NET Framework 4.5
Copyright (C) Microsoft Corporation. All rights reserved.
c: \ Users \ Emil \ Documents \ ITU \ 3 Semester \ BDSA Examn Project 2013 \ bdsa-project-2013 \ Program \ SSMDB \ WebClient \ Views \ Home \ SearchResults.cshtml (22,18): error CS0012: The type 'Model.MovieDto' is defined in the assembly that is not referenced to. Add a reference to the assembly 'Model, Version = 0.0.0.0, Culture = neutral, PublicKeyToken = null'.
c: \ Users \ Emil \ AppData \ Local \ Temp \ Temporary ASP.NET Files \ root \ 8eddbb6c \ 32280fad \ assembly \ dl3 \ 895f125b \ 5996f043_56f8ce01 \ Webclient.dll: (Location of symbol related to previous error)
c: \ Users \ Emil \ Documents \ ITU \ 3 Semester \ BDSA Examn Project 2013 \ bdsa-project-2013 \ Program \ SSMDB \ WebClient \ Views \ Home \ SearchResults.cshtml (24,54): error CS1061: 'Model.MovieDto' does not contain a definition for 'Title', and there was not found an extension method 'Title' accepting a first argument of type 'Model.MovieDto' (are you missing a "using' directive or an assemblyreference?)
我尝试重新添加对Model类的引用(它位于sollution中,但是在另一个项目中。我是Razor和ASP.NET的新手,所以我有点迷失在这里。我估计它可能是我的Web.config文件中的错误或我的ASP.NET临时文件中的错误,但尝试通过这两种尺寸解决问题已经无处可去。然后再次,因为我对此非常新,它可能也就像我做错事一样简单。不过要提前感谢。
答案 0 :(得分:0)
要修复错误,请尝试添加程序集,其中dto类声明为web.config中的编译程序集(请参阅Fixing Error CS0012)。但...
使用静态控制器字段将数据从控制器传递到视图并不是一个好主意。您将如何处理多个同时请求?因此,使用ViewBag
或创建适当的模型并将其传递给视图:
public class SearchResultModel
{
public IList<Model.MovieDto> Movies { get; set; }
public IList<Model.PersonDto> People { get; set; }
}
创建并传递
public ActionResult SearchResults(string input)
{
ViewBag.Title = "Search";
ViewBag.Message = "Your search returned the following match(es).";
var results = Model.ModelController.UniversalSearch(input);
var model = new SearchResultModel {
Movies = results.MovieListForUser,
People = results.PersonListForUser
};
return View(model);
}
查看:
@model Your.Namespace.SearchResultModel
...
<td>
<div id="movieResults">
@foreach (var movie Model.Movies) {
<span class="movieResult"> @movie.Title </span><br/>
}
<span>Total movie hits: @Model.Movies.Count</span>
...
每个Web请求都会导致单独创建模型。