2012-02-21 17 views
5

Sto cercando di utilizzare il mio modello in modalità rasoio e sto ottenendo l'errore sopra specificato. Il codice predefinito (Account \ ChangePassword.cshtml, ... ecc.) Generato in MVC3 contenente la parola chiave @model funziona correttamente, solo la mia nuova vista sta avendo il problema.MVC3: messaggio di errore del compilatore: CS0103: il nome 'modello' non esiste nel contesto corrente

Il mio codice nel file di cshtml è la seguente:

@model VidLib.Models.LogedInUsersModel 


@{ 
    ViewBag.Title = "LogedInUsers"; 
} 

<h2>LoggedInUsers</h2> 

@foreach (MembershipUser user in model.GetUsers()) 
{ 
    <p>Name: @user.UserName 
    </p> 
} 

Il mio codice del modello è la seguente:

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(); 
     } 

    } 

} 

L'errore che sto ottenendo è la seguente:

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 

risposta

6

controlla il codice nel tuo file view (LogedInUsers.cshtml). di Esso modello, non modellare

@foreach (utente MembershipUser in Model.GetUsers()) {

+0

Ora sto ottenendo l'attesa nulla quando si utilizza Modello anziché modello. – coolcake

+0

si dovrebbe gestire null se non ci sono dati. i.e Membership.GetAllUsers() non ritraccia alcun dato di ruolo. quindi gestisci l'eccezione nulla. – MikMark

1

C'erano due problemi nella mia applicazione.

1) Il modello avrebbe dovuto essere utilizzato in Visualizza anziché in modello, come suggerito da Mik-Mark. 2) Devo passare il mio modello a View nel controller che non stavo facendo.

[Authorize(Roles = "Admin")] 
public ActionResult LogedInUsers() 
{ 
     var model = new LogedInUsersModel(); 
     return View(model); 
} 
2

Presumo che si restituiscano più record dal modello. Non ho provato l'associazione del modello a un metodo, non sono sicuro che funzioni. Proverei una proprietà Il modello

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; } 
} 

controllore

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); 
} 

vista

@model MyUserApp.ViewModels.UserViewModel 

foreach(User u in Model.Users) 
{ 
    Name: @u.UserName<br/> 
} 

Prova legame proprietà anziché metodi. Si dovrebbe anche considerare l'associazione a un ViewModel piuttosto che al modello che è associato al database. Quando si associa la vista direttamente al modello o al repository del datastore, si accoppia strettamente il modello alla vista. Il modello è quindi associato sia alla vista che al database (presupponendo che si stia utilizzando il framework di entità e le annotazioni di dati). Ciò può causare molti errori di convalida dei dati quando si tenta di convalidare un modello contro la vista e il database. Per esempio, se si aggiungono questi attributi al modello

[Required(ErrorMessage="User name is required")] 
[StringLenth(50)] // this causes validation against the database and the view 
public string UserName { get; set; } 

Come regola generale, l'annotazione uso di dati attributi per aiutare a definire la base di dati nel modello e poi fare la convalida UI nel ViewModel. Se si aggiungono gli stessi attributi al modello di vista, si sta convalidando solo contro la vista.

Problemi correlati