Solved

Separate Models in ASP.NET MVC

Posted on 2009-05-07
10
456 Views
Last Modified: 2013-11-08
I've got a Users table, and a Password table. The password table references the Users table, because each time the user creates/changes their password, the old one is kept in log and encrypted for logging reasons.

I'm a bit new to MVC, so let me show you what the problem is. I'm confused as to how I bind this to my Create View.

Any ideas on how I should approach this with the view page?
LinqSchema01.jpg
UserController.jpg
ViewPage.jpg
0
Comment
Question by:veruthandai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24333155
The Model should have the data, the View should display the model, and the controller should have a reference to the model and the view.
0
 
LVL 21

Accepted Solution

by:
MogalManic earned 500 total points
ID: 24334936
I would  create a model that reads in the User and Password joined together.  When a new userName/Password is commited to the database,  the presenter will make two calls to the model:
   UserModel.CreateUser(id, Name, Email)
   UserModel.CreatePassword(id, PasswordHash, UserID);  
0
 

Author Comment

by:veruthandai
ID: 24335045
@TheLearnedOne

Yes, I understand what you mean, but the problem is that the View references the User Model - and the "Password" model is a different model entirely.

@MogalManic
This isn't a bad approach, but I can't believe this is how people typically handle this in MVC - seeing as it would be an extremely common thing with the way ORM is designed. There is no way to reference a secondary model within a view?
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 96

Expert Comment

by:Bob Learned
ID: 24335632
With MVP/MVC, the model and the view should be separate.  The controller should be the go-between for the model and the view.  I have seen some implementations where the view and controller are combined, but I believe that goes against the MVP/MVC grain.  The model and the view are independent so that you can have multiple views on the same model, or define the same pattern for both a Windows Forms or web site application.
0
 

Author Comment

by:veruthandai
ID: 24335781
Yes, but the Password is part of the Model, but not a static part of it. It is like an array that is attached to the Model. So it is still part of the model. What I need to figure out how to do is to bind one of the text fields so that it will validate with the password object.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24336000
The controller can define an instance of the model, get to the password, and validate it.  The controller should have the business logic, and not the view or the model.
0
 

Author Comment

by:veruthandai
ID: 24336037
Alright, but how does one do that? How do I create an instance of the password to pass to the view when I can only define the View to handle an instance of a User? That's more what I'm getting at. I think we're both trying to say the same thing, except I don't understand where to go from just understanding what has to happen.
0
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24336058
Is the model a single class, or a collection of mutiple classes?
0
 

Author Comment

by:veruthandai
ID: 24336111
The model is a "User", there's a screenshot of the schema in the original post.
User
- Id
- Name
 
Password
- Id
- Hash
- UserId
 
So User.Passwords would be a collection in the Model that is being passed to the View.
0
 
LVL 21

Expert Comment

by:MogalManic
ID: 24336228
For simplicity, I would add a CurrentPassword property which would be something like this:
public string CurrentPassword
{
  get {return this.Passwords[0];}
  set {this.Passwords.Insert(0, value);  //Add new password, pushing old password into Passwords[1]}
}

That way on your View Page you just have to do this:
<% Html.Password(CurrentPassword, "*") %>
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Dynamics CRM Plugin Query TotalRecordCount returning -1 13 56
Where is this file? 3 33
"The resource cannot be found" 1 22
Visual Studio 2015 auto inserted code 12 68
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
The purpose of this video is to demonstrate how to set up the WordPress backend so that each page automatically generates a Mailchimp signup form in the sidebar. This will be demonstrated using a Windows 8 PC. Tools Used are Photoshop, Awesome…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question