?
Solved

Having trouble putting the Model into the Model-View-Controller

Posted on 2009-04-25
4
Medium Priority
?
150 Views
Last Modified: 2013-12-17
I have created a new application using the ASP.NET MVC Web Application, which contains a Models folder, with nothing inside. When I right click, the item in the list which I asume I should chose is Class. But when I do so I get an empty class and no guidance from Visual Studio (2008).  This is correct behavior, but I am just trying to show you why I am stuck.

On the other hand, if I want Visual Studio to hand hold me and generate everything, the thing to choose is ADO.NET Entitiy Data Model. This handles everything, and works fine.  But since it puts all the funtion calls into Designer.cs, which is machine generated, I then have a hard time trying to use it as an example how to properly make the conection from the Controller to the Model.  So I am not able to replicate an existing function call and create my own calls, which is what I'd like to learn how to do.

Could someone please give me some function headers (and references) and show me where they belong?   By this I mean what goes into the Model, the View and the Controller, to create some very simple communication.  


Thanks,
newbieweb
0
Comment
Question by:newbieweb
  • 2
  • 2
4 Comments
 
LVL 96

Expert Comment

by:Bob Learned
ID: 24247211
Model example:

public class ProductModel
{

    public int ProductID { get; private set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }

    public ProductModel(int id, string name, string description)
    {
         this.ProductID = id;
         this.ProductName = name;
         this.ProductDescription = description;
    }
}
0
 

Author Comment

by:newbieweb
ID: 24247338
That looks easy.  I assume I would then just add the Observer pattern and register both the View and the Controller observers.  And I'd be on my way.

The Controller can interact with the Model using public properties and methods.

Is there anything else I need?  

Thanks.
0
 
LVL 96

Accepted Solution

by:
Bob Learned earned 2000 total points
ID: 24252016
Slight modification for Observer pattern:


    public class ProductModel
    {
 
        public event PropertyChangedEventHandler PropertyChanged;
 
        public ProductModel(int id, string name, string description)
        {
            this.ProductID = id;
            this.ProductName = name;
            this.ProductDescription = description;
        }
 
        public int ProductID { get; private set; }
 
        private string _productName = "";
 
        public string ProductName
        {
            get
            {
                return _productName;
            }
            set
            {
                _productName = value;
                this.OnPropertyChanged("ProductName");
            }
        }
 
        private string _productDescription = "";
 
        public string ProductDescription
        {
            get
            {
                return _productDescription;
            }
            set
            {
                _productDescription = value;
                this.OnPropertyChanged("ProductDescription");
            }
        }
 
        // Raise the PropertyChanged event to notifier caller that something changed the model.
        private void OnPropertyChanged(string propertyName)
        {
            if (this.PropertyChanged != null)
                this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

Open in new window

0
 

Author Closing Comment

by:newbieweb
ID: 31574595
That really helped tie the whole thing together for me.

Thanks!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …
Suggested Courses

830 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