I’ve started learning MVC3 development and have hit a bit of a stumbling block. In my project I’ve moved the default aspnet_ membership tables into a separate SQL Server 2008 Express database. I then added my own tables to hold extra profile data. I’d like to now build a view that will list usernames (from aspnet_Users), email addresses (from aspnet_Membership), First and Last Name (from custom built table UserDetails with userid as PK and FK to aspnet_Users) and the role of users (aspnet_UsersInRoles and aspnet_Roles).
So far to read and write from my database I’ve built models for each of my custom built tables with a dbcontext class:
public class UserDetail
{
[Key]
public Guid UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set; }
public string Zip { get; set; }
public string Phone { get; set; }
public DateTime DateUpdated { get; set; }
}
public class UserLinkToken
{
[Key]
public Guid UserId { get; set; }
public string LinkToken { get; set; }
public DateTime DateUpdated { get; set; }
}
public class ProShotDB : DbContext
{
public DbSet<UserDetail> UserDetails { get; set; }
public DbSet<UserLinkToken> UserLinkTokens { get; set; }
}
I’ve had to name my tables “UserDetails” and “UserLinkTokens” for my app to write and update my database, when the names of models and dbcontext didn’t match my database and table names the app would end up creating another database with tables and write the data there.
From what I can tell to send an object to view with properties from aspnet_ tables and properties from my custom tables I will want to build a ModelView. After reading through lots of modelview examples I’m still not clear on how it should look for my app.
Could someone give me an example of a modelview that I could use to display a list of data rows and update all the respective table columns referenced in the modelview given a userid?
For the example lets say I just wanted a view of all the FirstNames (UserDetails table) and their corresponding Emails (aspnet_Membership).
My ultimate goal is a view that will display the users with their assigned roles. Each user listed will have a link that will lead to a controller that will assign them as administrator if they are not currently of that role or remove the administrator role if they are currently an administrator.
Sorry if this type of question has been answered before in other questions. I’ve read quite a few of them and feel like they probably should have answered my question but none seemed to quite match. Most likely my confusion stems from not fully understanding how my app is making the connections to my database. I sort of understand that my models represent a row of data for a table and the dbcontext takes a set of these data row models and connects it to the table in the database (dbset<(datarow/model object)> (table of datarows / set of model objects)). Since I don’t see a dbcontext class for the aspnet_ tables/model objects I don’t have a good concept of how data in those tables are retrieved, updated or written.
Thanks for any help clearing up my ignorance on how this works or giving me a simple example of how to accomplish what I need.
First, it’s called a View Model, not a ModelView. A View Model is simply a class that is customized for the purposes of a view. It typically contains only the data needed for that view, and contains things like Data Annotations to deal with validation and presentation.
Thus, you just create a model with all the data you want displayed in your view. That’s all there is to building a view model.
So your view model would simply be
Then in your view, you just do something like this:
In your controller (or better yet, in a business layer) you would simply do a query to retrieve the users names and emails, then copy the values from your query results into the view model.
You do the reverse when you update the data. This is really too large a concept though to discuss in a post like this. You should really look at example code, such as MVC Music Store, or Nerd Dinner.
The important thing to keep in mind. Membership is a completely separate system from your other tables. You must retrieve them separately, and update them separately. Use the Membership API to update the members email or other details, and use your own tables to update your data.