Presentation is loading. Please wait.

Presentation is loading. Please wait.

Social Media And Global Computing Managing MVC with Custom Models

Similar presentations


Presentation on theme: "Social Media And Global Computing Managing MVC with Custom Models"— Presentation transcript:

1 Social Media And Global Computing Managing MVC with Custom Models

2 The Role of Models in MVC
Models manage your application’s persistent data storage, which is essential to the logic of both the views and the controllers. The MVC Pattern’s Separation of Concerns specifies where each kind of logic should be located in the application: The UI logic belongs in the view. Input logic belongs in the controller. Business logic belongs in the model.

3 The Role of Models in MVC
Models manage the persistent data storage, which is essential to both views and controllers

4 The Role of Models in MVC
Typical user interaction with MVC app: User makes a request on a view / form Controller handles request with view function Controller interacts with model Controller selects new view as response The new view awaits another user action

5 The Role of Models in MVC
View doesn’t directly interact with model Controller provides form of model to view View displays data in user interface View transmits data to controller for update

6 Building Models Views should be simple as possible
There to display what the user needs now Design models for the needs of the views Put complicated logic into model, not the view Controller functions should be simple Put complicated logic into model, not function Controller function translates user actions to the model then provides a response

7 Building Models Two basic approaches: Custom Model Class
Entity Framework Model Class

8 The CRUD Functions When you are building a Model for storage of user data, you should usually implement the CRUD functions: Create – for creating data elements Ex: The Create() View Function Retrieve – for giving details of or listing elements Ex: The Details() and List() View Functions Update – for editing data elements Ex: The Edit() View Function Delete – for removing data elements Ex: The Delete() View Function

9 Building Custom Models
We will create a Dog data structure (Model) that will consist of three attributes – ID, Name, Age: public class Dog { public int ID { get; set; } public string Name { get; set; } public int Age { get; set; } }

10 Building Custom Models
When you have placed your class in your Models folder, you add the following “using” directive to access it from your Controller : using [ProjectName].Models;

11 Building Controller Functions for Custom Models
The Dog Model’s data object and its List and Details methods: public static List<Dog> dogs = new List<Dog>(); public ActionResult List() { return View(dogs); } public ActionResult Details(Dog dog) return View(dog);

12 Building Controller Functions for Custom Models
The Dog Model’s Create methods: public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(Dog dog) if (!ModelState.IsValid) return View("Create", dog); dogs.Add(dog); return RedirectToAction("List");

13 Building Controller Functions for Custom Models
public ActionResult Edit(int ID) { Entry theEntry = null; ViewBag.Message = "Edit the following Entry’s Information:"; foreach (Entry e in entries) if (d.ID == ID) theEntry = e; break; } if (theEntry == null) return RedirectToAction("Error"); else return View(theEntry);

14 Building Controller Functions for Custom Models
[HttpPost] public ActionResult Edit(Entry entry) { if (!ModelState.IsValid) ViewBag.Message = "There was an editing error:"; return View("Edit", entry); } foreach (Entry e in entries) if (e.ID == entry.ID) e.Name = entry.Name; e.Phone = entry.Phone; e. = entry. break; return RedirectToAction("List");

15 Building Controller Functions for Custom Models
public ActionResult Delete(int ID) { Entry theEntry = null; ViewBag.Message = "Delete the following Entry's Information:"; foreach (Entry e in entries) if (e.ID == ID) theEntry = e; break; } if (theEntry == null) return RedirectToAction("Error"); else return View(theEntry);

16 Building Controller Functions for Custom Models
[HttpPost] public ActionResult Delete(Entry entry) { if (entry == null) return RedirectToAction("Error"); foreach (Entry e in entries) if (e.ID == entry.ID) entries.Remove(e); break; } return RedirectToAction("List");

17 Using the Default Error Page
In the Controller, you can use the Default Error page that is inside the Shared folder if you include the following Action method: public ActionResult Error() { return View(); }

18 Building Views for the Custom Models
To create your views for the Controller functions of your Custom Model, you need to choose “Create a strongly-typed view” in the “Add view” dialog, and then select whatever appropriate Model “View Content” applies.

19 Building Views for the Custom Models
When you have a view that is a strongly-typed you have the option of having views created for you that have logic in them to handle Creating, Listing, Deleting, or managing other manipulations of that data in the view.

20 Building Views for the Custom Models
When you have a view that is a strongly-typed to a model view, you can pass the model directly back and forth between the view and controller. You can also use the ModelState data to determine if a valid Model is being received.


Download ppt "Social Media And Global Computing Managing MVC with Custom Models"

Similar presentations


Ads by Google