我正在尝试在剃刀视图中使用我的模型,我收到上面指定的错误。在包含@model关键字的MVC3中生成的默认代码(Account \ ChangePassword.cshtml,...等)工作正常,只有我的新视图出现问题。
我在cshtml文件中的代码如下:
@model VidLib.Models.LogedInUsersModel
@{
ViewBag.Title = "LogedInUsers";
}
<h2>LoggedInUsers</h2>
@foreach (MembershipUser user in model.GetUsers())
{
<p>Name: @user.UserName
</p>
}
我的型号代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Security;
using System.ComponentModel.DataAnnotations;
namespace VidLib.Models
{
public class LogedInUsersModel
{
public MembershipUserCollection GetUsers()
{
return Membership.GetAllUsers();
}
}
}
我得到的错误如下:
Server Error in '/VTest' Application.
--------------------------------------------------------------------------------
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'model' does not exist in the current context
Source Error:
Line 8: <h2>LoggedInUsers</h2>
Line 9:
Line 10: @foreach (MembershipUser user in model.GetUsers())
Line 11: {
Line 12: <p>Name: @user.UserName
Source File: d:\test projects\VidLib\VidLib\Views\Administrator\LogedInUsers.cshtml Line: 10
Show Detailed Compiler Output:
C:\WINDOWS\system32> "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\csc.exe" /t:library /utf8output /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.Routing\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.Routing.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System\v4.0_4.0.0.0__b77a5c561934e089\System.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.ApplicationServices\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.ApplicationServices.dll" /R:"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vtest\9fce5a68\47e852d4\assembly\dl3\81c380d9\4b4be05b_5ff0cc01\VidLib.DLL" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.Helpers\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.Helpers.dll" /R:"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\mscorlib.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Configuration\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Drawing\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel\v4.0_4.0.0.0__b77a5c561934e089\System.ServiceModel.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ComponentModel.DataAnnotations\v4.0_4.0.0.0__31bf3856ad364e35\System.ComponentModel.DataAnnotations.dll" /R:"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vtest\9fce5a68\47e852d4\App_global.asax.4-dk6yw3.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Data\v4.0_4.0.0.0__b77a5c561934e089\System.Data.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Data.DataSetExtensions\v4.0_4.0.0.0__b77a5c561934e089\System.Data.DataSetExtensions.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Core\v4.0_4.0.0.0__b77a5c561934e089\System.Core.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.DynamicData\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.DynamicData.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Runtime.Serialization\v4.0_4.0.0.0__b77a5c561934e089\System.Runtime.Serialization.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.WorkflowServices\v4.0_4.0.0.0__31bf3856ad364e35\System.WorkflowServices.dll" /R:"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vtest\9fce5a68\47e852d4\assembly\dl3\09b97a44\38235bb7_32e5cc01\EntityFramework.DLL" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.Services\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.Services.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\Microsoft.CSharp\v4.0_4.0.0.0__b03f5f7f11d50a3a\Microsoft.CSharp.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Activities\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Activities.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Web\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Web.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.Mvc\v4.0_3.0.0.0__31bf3856ad364e35\System.Web.Mvc.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Activities\v4.0_4.0.0.0__31bf3856ad364e35\System.Activities.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.Abstractions\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.Abstractions.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Data.Entity\v4.0_4.0.0.0__b77a5c561934e089\System.Data.Entity.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml.Linq\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.Linq.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.Web\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.Web.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.IdentityModel\v4.0_4.0.0.0__b77a5c561934e089\System.IdentityModel.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.Extensions\v4.0_4.0.0.0__31bf3856ad364e35\System.Web.Extensions.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Xml\v4.0_4.0.0.0__b77a5c561934e089\System.Xml.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.Web.WebPages.Deployment\v4.0_1.0.0.0__31bf3856ad364e35\System.Web.WebPages.Deployment.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_MSIL\System.ServiceModel.Activation\v4.0_4.0.0.0__31bf3856ad364e35\System.ServiceModel.Activation.dll" /R:"C:\WINDOWS\Microsoft.Net\assembly\GAC_32\System.EnterpriseServices\v4.0_4.0.0.0__b03f5f7f11d50a3a\System.EnterpriseServices.dll" /out:"C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vtest\9fce5a68\47e852d4\App_Web_logedinusers.cshtml.e8fa2554.wfm-mxw5.dll" /D:DEBUG /debug+ /optimize- /w:4 /nowarn:1659;1699;1701 /warnaserror- "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vtest\9fce5a68\47e852d4\App_Web_logedinusers.cshtml.e8fa2554.wfm-mxw5.0.cs" "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\vtest\9fce5a68\47e852d4\App_Web_logedinusers.cshtml.e8fa2554.wfm-mxw5.1.cs"
Microsoft (R) Visual C# 2010 Compiler version 4.0.30319.1
Copyright (C) Microsoft Corporation. All rights reserved.
d:\test projects\VidLib\VidLib\Views\Administrator\LogedInUsers.cshtml(10,34): error CS0103: The name 'model' does not exist in the current context
Show Complete Compilation Source:
Line 1: #pragma checksum "D:\test projects\VidLib\VidLib\Views\Administrator\LogedInUsers.cshtml" "{406ea660-64cf-4c82-b6f0-42d48172a799}" "1D2E7C5249B30EC171BB04A575153072"
Line 2: //------------------------------------------------------------------------------
Line 3: // <auto-generated>
Line 4: // This code was generated by a tool.
Line 5: // Runtime Version:4.0.30319.1
Line 6: //
Line 7: // Changes to this file may cause incorrect behavior and will be lost if
Line 8: // the code is regenerated.
Line 9: // </auto-generated>
Line 10: //------------------------------------------------------------------------------
Line 11:
Line 12: namespace ASP {
Line 13: using System;
Line 14: using System.Collections.Generic;
Line 15: using System.IO;
Line 16: using System.Linq;
Line 17: using System.Net;
Line 18: using System.Web;
Line 19: using System.Web.Helpers;
Line 20: using System.Web.Security;
Line 21: using System.Web.UI;
Line 22: using System.Web.WebPages;
Line 23: using System.Web.Mvc;
Line 24: using System.Web.Mvc.Ajax;
Line 25: using System.Web.Mvc.Html;
Line 26: using System.Web.Routing;
Line 27:
Line 28:
Line 29: public class _Page_Views_Administrator_LogedInUsers_cshtml : System.Web.Mvc.WebViewPage<VidLib.Models.LogedInUsersModel> {
Line 30:
Line 31: #line hidden
Line 32:
Line 33:
Line 34: public _Page_Views_Administrator_LogedInUsers_cshtml() {
Line 35: }
Line 36:
Line 37: protected ASP.global_asax ApplicationInstance {
Line 38: get {
Line 39: return ((ASP.global_asax)(Context.ApplicationInstance));
Line 40: }
Line 41: }
Line 42:
Line 43: public override void Execute() {
Line 44:
Line 45: WriteLiteral("\r\n\r\n");
Line 46:
Line 47:
Line 48:
Line 49: #line 4 "D:\test projects\VidLib\VidLib\Views\Administrator\LogedInUsers.cshtml"
Line 50:
Line 51: ViewBag.Title = "LogedInUsers";
Line 52:
Line 53:
Line 54:
Line 55: #line default
Line 56: #line hidden
Line 57: WriteLiteral(" \r\n<h2>LoggedInUsers</h2>\r\n\r\n");
Line 58:
Line 59:
Line 60:
Line 61: #line 10 "D:\test projects\VidLib\VidLib\Views\Administrator\LogedInUsers.cshtml"
Line 62: foreach (MembershipUser user in model.GetUsers())
Line 63: {
Line 64:
Line 65:
Line 66: #line default
Line 67: #line hidden
Line 68: WriteLiteral(" <p>Name: ");
Line 69:
Line 70:
Line 71:
Line 72: #line 12 "D:\test projects\VidLib\VidLib\Views\Administrator\LogedInUsers.cshtml"
Line 73: Write(user.UserName);
Line 74:
Line 75:
Line 76: #line default
Line 77: #line hidden
Line 78: WriteLiteral(" \r\n </p>\r\n");
Line 79:
Line 80:
Line 81:
Line 82: #line 14 "D:\test projects\VidLib\VidLib\Views\Administrator\LogedInUsers.cshtml"
Line 83: }
Line 84:
Line 85:
Line 86: #line default
Line 87: #line hidden
Line 88: WriteLiteral("\r\n");
Line 89:
Line 90:
Line 91: }
Line 92: }
Line 93: }
Line 94:
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
答案 0 :(得分:6)
检查视图中的代码(LogedInUsers.cshtml)文件。 它是模型,而不是模型
@foreach(Model.GetUsers()中的MembershipUser用户){
答案 1 :(得分:2)
我假设您从模型中返回多条记录。我没有尝试过模型绑定到方法,我不确定它是否有效。我会尝试一下房产。模型
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
}
ViewModel
public class UserViewModel { get; set; }
{
public List<User> UserList { get; set; }
}
控制器
DBContext db = new DBContext();
public ViewResult ()
{
List<Users> users = db.Users.Where(m => m.LoggedIn == true).ToList();
UserViewModel userVM = new UserViewModel();
userVM.UserList = users;
return View(userVM);
}
视图
@model MyUserApp.ViewModels.UserViewModel
foreach(User u in Model.Users)
{
Name: @u.UserName<br/>
}
尝试绑定属性而不是方法。您还应该考虑绑定到ViewModel而不是绑定到数据库的Model。将视图直接绑定到数据存储模型或存储库时,您将模型与视图紧密耦合。然后,模型绑定到视图和数据库(假设您正在使用实体框架和数据注释)。当您尝试针对视图和数据库验证一个模型时,这可能会导致大量数据验证错误。例如,如果将这些属性添加到模型
[Required(ErrorMessage="User name is required")]
[StringLenth(50)] // this causes validation against the database and the view
public string UserName { get; set; }
通常,使用数据注释属性来帮助在模型中定义数据库,然后在ViewModel中进行UI验证。如果向视图模型添加相同的属性,则仅对视图进行验证。
答案 2 :(得分:1)
我的申请中有两个问题。
1)模型应该已经在View中使用,而不是像Mik-Mark建议的那样使用模型。 2)我必须将我的模型传递给控制器中的View,我没有这样做。
[Authorize(Roles = "Admin")]
public ActionResult LogedInUsers()
{
var model = new LogedInUsersModel();
return View(model);
}