I am trying to use my model in razor view and I am getting the above specified error. The default code (Account\ChangePassword.cshtml,...etc)generated in MVC3 containing @model keyword is working fine, only my new view is having the problem.
My code in cshtml file is as follows:
@model VidLib.Models.LogedInUsersModel
@{
ViewBag.Title = "LogedInUsers";
}
<h2>LoggedInUsers</h2>
@foreach (MembershipUser user in model.GetUsers())
{
<p>Name: @user.UserName
</p>
}
My model code is as follows:
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();
}
}
}
The error I am getting is as follows:
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
check the code in your view(LogedInUsers.cshtml) file. It's Model ,not model
@foreach (MembershipUser user in Model.GetUsers()) {
I assume that you are returning multiple records from your model. I have not tried model binding to a method, I am not sure it works. I would try a property. The model
public class User
{
public int UserId { get; set; }
public string UserName { get; set; }
}
the ViewModel
public class UserViewModel { get; set; }
{
public List<User> UserList { get; set; }
}
the controller
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);
}
the view
@model MyUserApp.ViewModels.UserViewModel
foreach(User u in Model.Users)
{
Name: @u.UserName<br/>
}
Try binding to properties rather than methods. You should also consider binding to a ViewModel rather than your Model which is bound to the database. When you bind the view directly to the datastore model or repository, you are tightly coupling the model to the view. The model then is bound to both the view and the database (assuming you are using the entity framework and data annotations). This can cause a lot of data validation errors when you try to validate one model against the view and the database. For example if these attributes are added to the model
[Required(ErrorMessage="User name is required")]
[StringLenth(50)] // this causes validation against the database and the view
public string UserName { get; set; }
As a rule, use data annotation attributes to help define the database in your model and then do UI validation in the ViewModel. If you add the same attributes to the view model, you are validating only against the view.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With