Data Structures and Database Applications Custom Models with MVC
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.
The Role of Models in MVC Models manage the persistent data storage, which is essential to both views and controllers:
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
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
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
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
Building Custom Models An Entry data structure (Model) that consist of four attributes – ID, Name, Phone, Email: public class Entry { public int ID { get; set; } public string Name { get; set; } public string Phone { get; set; } public string Email { get; set; } }
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;
Building Controller Functions for Custom Models The Entry Model’s data object and its List and Details methods: public static List<Entry> entries = new List<Entry>(); public ActionResult List() { return View(entries); } public ActionResult Details(Entry entry) return View(entry);
Building Controller Functions for Custom Models The Entry Model’s Create methods: public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(Entry entry) if (!ModelState.IsValid) return View("Create", entry); entries.Add(entry); return RedirectToAction("List");
Building Views for the Custom Models To create your views for the Controller functions of your Custom Model, you need to identify the appropriate CRUD template in the “Add view” dialog and then choose the appropriate “Model Class” on the line under the template.
Building Views for the Custom Models When you have a view that is associated with a model, then the CRUD templates logic is added to that view to handle Creating, Listing, Deleting, or managing other manipulations of that data in the view.
Building Views for the Custom Models When you have a view that is associated with a model, you can pass an object from that model directly back and forth between the view and controller.
Example: Creating Entries An Example of CRUD Functions for views that are for creating a list of entries: public static List<Entry> entries = new List<Entry>(); public static int uniqueID = 1; Function public ActionResult List() { ViewBag.Message = "Here's the list of the Entries:"; return View(entries); } public ActionResult Details(Entry entry) ViewBag.Message = "Details of this entry:"; return View(entry);
The CRUD Functions (cont.) public ActionResult Create() { ViewBag.Message = "Fill in the details for this entry:"; return View(); } [HttpPost] public ActionResult Create(Entry entry) if (!ModelState.IsValid) ViewBag.Message = "There was a creation error:"; return View("Create", entry); entry.ID = uniqueID++; entries.Add(entry); return RedirectToAction("List");
The CRUD Functions (cont.) 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);
The CRUD Functions (cont.) [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.Email= entry.Email break; return RedirectToAction("List");
The CRUD Functions (cont.) 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);
The CRUD Functions (cont.) [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");