MVC3 default properties properties with custom model properties (use ModelView?)

I began to study MVC3 development and hit a stumbling block a bit. In my project, I moved the default aspnet_ tables to a separate SQL Server 2008 Express database. Then I added my own tables to store additional profile data. Now I would like to create a view that lists usernames (from aspnet_Users), email addresses (from aspnet_Membership), First and Last Name (from the UserDetails user table with userid as PK and FK in aspnet_Users) and the user role (aspnet_UsersInRoles and aspnet_Roles).

So far, reading and writing from my database, I have built models for each of my custom tables with the 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 had to call my tables "UserDetails" and "UserLinkTokens" for my application to write and update my database, when the names of the models and dbcontext did not match the names of my database and tables, the application would finish creating another database with tables and write there is data.

From what I can say, to send an object for viewing using the properties from the tables and the aspnet_ properties from my user tables, I want to create a ModelView. After reading many examples of the model, I still do not understand how this should look for my application.

Can someone give me an example of a model that I could use to display a list of data rows and update all the relevant columns of the table referenced in the model view, taking into account the user ID?

In this example, we can say that I just wanted to see all the FirstNames tables (UserDetails) and their corresponding letters (aspnet_Membership).

My ultimate goal is a presentation that displays users with assigned roles. Each registered user will have a link that will lead to a controller that will designate them as an administrator if they are not currently in this role or remove the administrator role if they are currently administrators.

Sorry if this question was asked earlier in other questions. I read a lot of them and I feel that they probably should have answered my question, but none of them seemed to match. Most likely, my confusion stems from an incomplete understanding of how my application connects to my database. I understand that my models are a data row for a table, and dbcontext takes a set of these data row models and connects them to a table in the database (dbset <(datarow / model object)> (datarows table / set of model objects)). Since I don't see the dbcontext class for aspnet_ tables / model objects, I don't have a good concept on how to retrieve, update, or write data in these tables.

Thanks for any help, clarifying my ignorance about how this works or gives me a simple example of how to accomplish what I need.

+4
source share
2 answers

At first it is called View Model, not ModelView. A view model is simply a class configured for presentation purposes. It usually contains only the data needed for this view, and contains things like Data Annotations to validate and present the view.

Thus, you simply create a model with all the data that you want to display in your view. That's all it takes to create a view model.

This way your look model will simply

 public class MyViewModel { public string FirstName {get;set;} public string Email {get;set;} } 

Then, in your opinion, you just do something like this:

 @model MyViewModel @using(Html.BeginForm()) { @Html.EditorForModel() <input type="submit" /> } 

In your controller (or, even better, at the business level), you simply run the query to get usernames and emails, and then copy the values ​​from the query results into the view model.

When updating data, you do the opposite. This is really too big a concept, although for discussion in such a publication. You really should look at sample code, such as the MVC Music Store or Nerd Dinner.

Important to remember. Membership is a completely separate system from your other tables. You must get them separately and upgrade them separately. Use the membership API to update members email or other details and use your own tables to update your data.

+1
source

Well, I decided that all I really needed for this function was membership, so I gave up trying to include First and Last Name.

My ViewModel:

 namespace Pro_Shot_Portal.ViewModels { public class SetAdminViewModel { public string UserName { get; set; } public string Email { get; set; } public string[] Role { get; set; } } } 

My controllers:

  [Authorize(Roles = "Administrator")] public ActionResult SetRoles() { var model = Membership .GetAllUsers() .Cast<MembershipUser>() .Select(x => new SetAdminViewModel { UserName = x.UserName, Email = x.Email, Role = Roles.GetRolesForUser(x.UserName) }); return View(model); } [Authorize(Roles = "Administrator")] public ActionResult AddAdmin(string username) { Roles.AddUserToRole(username, "Administrator"); return RedirectToAction("SetRoles", "Account"); } [Authorize(Roles = "Administrator")] public ActionResult RemoveAdmin(string username) { Roles.RemoveUserFromRole(username, "Administrator"); return RedirectToAction("SetRoles", "Account"); } 

My view:

 @model IEnumerable<Pro_Shot_Portal.ViewModels.SetAdminViewModel> @{ ViewBag.Title = "Set Roles"; } <h2>Index</h2> <table> <tr> <td>UserName</td> <td>Email</td> <td>Roles</td> </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.UserName) </td> <td> @Html.DisplayFor(modelItem => item.Email) </td> <td> @Html.DisplayFor(modelItem => item.Role) </td> <td> @if (@Html.DisplayFor(modelItem => item.Role).ToString() == "Administrator") { @Html.ActionLink("Remove Admin", "RemoveAdmin", new { username = item.UserName }) } else { @Html.ActionLink("Add Admin", "AddAdmin", new { username = item.UserName }) } </td> </tr> } </table> 

Now it works for me.

Thanks for helping me get Mystere on the right track. Also, please let me know if something is wrong with my code.

0
source

Source: https://habr.com/ru/post/1441167/


All Articles