Reputation: 5029
I lack understanding of some basic MVC concepts, despite all my searching.
I created an MVC project in Visual Studio, which contains the partial view _LogOnPartial.shtml. I just want to access information within the view pertaining to the user, to put in a user dropdown menu. When I try to put this at the top of the partial view cshtml page I get the above error:
@model MyProject_MVC.Models.UserRepository
When I try this I also get an error:
@Html.Partial("_LogOnPartial", MyProject_MVC.Models.UserRepository)
'MyProject_MVC.Models.UserRepository' is a 'type', which is not valid in the given context
Upvotes: 2
Views: 3308
Reputation: 38468
You have to provide an instance of MyProject_MVC.Models.UserRepository
to the partial view. _LogOnPartial
is strongly-typed to that type. It lets you access its public members at compile time and decide how you can display it in your view.
If you want to use your own type in that view, first you have to change the type that is strongly-typed to it.
@model MyProject_MVC.Models.UserRepository
Then you have to create an instance of that type in your action method and pass it to the view as the model or a property of the model.
public ActionResult LogOn()
{
return View(new UserRepository());
}
Now you can access this instance as Model object in your view.
@Html.Partial("_LogOnPartial", Model);
Upvotes: 2
Reputation:
@model _LogOnPartial.Models.UserRepository
should probably be: @model MyProject_MVC.Models.UserRepository
and for the last part, you have to provide and instance of the type UserRepository as the second parameter, not the type itself.
Upvotes: 2