How mvc will work in the following case -C #
K, so last year I created a family tree program in java as a group project and we used MVC, but I really forgot how it works and the notes are no longer online.
To make things worse, since our project was not huge, the professor said to combine the model and controller.
So now that I'm trying to make a nice project to add to my portfolio of job opportunities, I don't know how MVC works.
What my relationship should look like between Model, View and Controller in the following cases, I could probably figure out the rest from this
- The model has a state called a location, and the view needs to know when this will change so it can update it.
- There is a button in the view that the model needs to know when it is clicked.
With those who decide, I will most likely do it myself.
Or, if anyone knows of any good information sites, I don't mind re-examining this.
a source to share
- The model has a state called a location and the view needs to know when this will change so that it can update it displays
Create an interface for the view called IMyScreen or something similar. Add an UpdateLocation method to the interface or something similar. Create a variable or properties that are visible for a state of type IMyScreen AND for a view. Call it something like MyScreen Have the View implement IMyScreen and put the code needed to update the view when the Location state changes in the UpdateLocation method. Your View initialization routine has the View register as the current MyScreen. Modify the code working with Location to run MyScreen.UpdateLocation at any time.
Ideally, the view would create a Command object that runs its execute method. The execute method will pull in any parameters it needs, change the location, and then call MyScreen.UpdateLocation. But you should focus on changing one thing at a time and save it for later.
- There is a button in the view that the model needs to know when it is clicked.
It's a bit vague. Usually such buttons perform an action or indicate a status (checkboxes, options, etc.)
If this is an action, then the ideal method is to create an ICommand interface. It has one Execute method. Use the initialization routine to pass any required parameters. Place the code required to modify the model in the execute method. When the button is clicked it will do something like
Sub MyButton_Click
ModifyCommand ThisCommand = New ModifyCommand(Parm1, Parm2, Parm3)
ModifyCommand.Execute
End Sub
If you need to query the status of a button, use the IMyScreen interface and add the ButtonClicked property. When the button is clicked, set the flag that was clicked in the view. When the model needs to know if the button was clicked, it is called MyScreen.ButtonClicked.
a source to share
Here's a great example in ASP.NET-MVC: http://www.codeplex.com/mvcsamples . The model is just data, the view is the UI code that shows the data, and the Controller decides which view to display.
a source to share