Download presentation
Presentation is loading. Please wait.
Published byCornelia Armstrong Modified over 8 years ago
1
Build Data Driven Apps with ASP.NET Core Rachel Appel
2
Agenda Overview of ASP.NET MVC Models Controllers Routing & REST Views ViewModels
3
ASP.NET MVC Models Views Controllers ViewModels
4
ASP.NET MVC Overview ASP.NET implementation of MVC MVC Pattern What about other patterns? MVVM Pattern, MVW, or MV* Patterns Routing RESTful
5
Models The application’s data Expressed in code as classes and their members Contains relationships Mapped to database objects
6
Models namespace Bakery.Models { public class Category { [Key] public int Id { get; set; } public string Name { get; set; } public virtual ICollection Products { get; set; } }
7
Models namespace Bakery.Models { public class Product { public int Id { get; set; } public string Name { get; set; } public string Description { get; set; } public string Image { get; set; } public decimal Price { get; set; } public DateTime ExpirationDate { get; set; } public int QuantityOnHand { get; set; } }
8
Entity Framework Code First Database First Model First DbSet Database Initializer (Code first) DBContext
9
Entity Framework public class BakeryContext : DbContext { public DbSet CartItem { get; set; } public DbSet Order { get; set; } public DbSet OrderDetails { get; set; } public DbSet ShoppingCart { get; set; } public DbSet Category { get; set; } public DbSet Products { get; set; } }
10
Entity Framework System.Data.Entity.Database.SetInitializer( new Models.DBContextInitializer()); In the global.asax.cs file
11
Entity Framework public class DBContextInitializer : DropCreateDatabaseAlways { protected override void Seed(MyContext context) { // fill db }
12
Controllers Match.com for Models and Views Traffic Cop Perform routing Accepts HTTP requests Front door of application Security
13
Controllers namespace Bakery.Controllers { public class ProductsController : Controller { private BakeryContext db = new BakeryContext(); public ActionResult Index() { return View(db.Products.ToList()); }
14
Controllers HTTP POST Data & HTTP Verbs [HttpPost] [ValidateAntiForgeryToken] public ActionResult Edit(Product product) { if (ModelState.IsValid) { db.Entry(product).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(product); }
15
Model Binding Matches model properties with HTTP Form values Default Model Binder Custom Model Binder Compound fields Calculated fields Dynamically created fields Fields that don’t map nicely to model
16
Routing / RESTful URLs REST : Representational State Transfer Request resources via RESTful URL
17
URLS Not RESTful http://books.com?isbn= 978-1500712952 http://shopping.com?category=2&subcategory=3 RESTful http://books.com/classics/jane-eyre http://shopping.com/computers/laptops
18
/products/ /products/details/cupcake /products/create /products/edit /products/delete/cupcake public class ProductsController : Controller { public ActionResult Index() {...} public ActionResult Details(string? name) {...} public ActionResult Create() {...} [HttpPost] public ActionResult Create(Product product) {} public ActionResult Edit(string? name) {...} [HttpPost] public ActionResult Edit(Product product) {...} public ActionResult Delete(string? name) {...} }
19
Routing public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute(name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }); }
20
Routing protected void Application_Start() { RouteConfig.RegisterRoutes(RouteTable.Routes); }
21
Views The UI/Presentation layer Renders a model or ViewModel Does not contain business logic A visualization of data Expects data from one source: a model or ViewModel Use HTML Helpers or custom HTML
22
Views Helpers Links Controls
23
Convention over Configuration Controller and action method name Matches the URL
24
Views @model IEnumerable @foreach (var item in Model) { @Html.DisplayFor(modelItem => item.Name) @Html.DisplayFor(modelItem => item.Description) @Html.DisplayFor(modelItem => item.Price) @Html.DisplayFor(modelItem => item.QuantityOnHand) @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | @Html.ActionLink("Details", "Details", new { id=item.Id }) | @Html.ActionLink("Delete", "Delete", new { id=item.Id }) }
25
Views and JavaScript Libraries Included in some VS Project Template You can use any 3 rd party JS library in MVC views jQuery & jQuery Mobile Bootstrap Modernizr Respond.js Angular
26
Views Validation Model attributes
27
Scaffolding Quickly create controllers and views based on models Great for prototyping Great for UI base
28
ViewModels A representation of one or more models Formatted & polished data UI logic code to format data One single ViewModel object per view Promotes SOC (Separation of Concerns)
29
ViewModels public class CustomerViewModel { public Customer Customer { get; set; } public StatesDictionary States { get; set; } public CustomerViewModel(Customer customer) { Customer = customer; States = new StatesDictionary(); }
30
Sumary Models, Views, Controllers Routing, Security, Deployment, OWIN
31
Rate This Session Now! Rate with Mobile App: Select the session from the Agenda or Speakers menus Select the Actions tab Click Rate Session Rate with Website: Register at www.devconnections.com/logintoratesession Go to www.devconnections.com/ratesession Select this session from the list and rate it Tell Us What You Thought of This Session Be Entered to WIN Prizes! #ITDevConnections
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.