900 likes | 1.08k Views
ASP.NET MVC. Tutoriál. Obsah. Založení projektu, přidání databáze Model Controllery, Routing a Pohledy Práce s daty (CRUD) Unit testy a Dependency injection. Založení projektu – co je třeba. Microsoft Visual Studio Mít nainstalovaný ASP.NET MVC. Založení projektu. Založení projektu.
E N D
ASP.NET MVC Tutoriál
Obsah • Založení projektu, přidání databáze • Model • Controllery, Routing a Pohledy • Práce s daty (CRUD) • Unit testy a Dependency injection
Založení projektu – co je třeba • Microsoft Visual Studio • Mít nainstalovaný ASP.NET MVC
Založení projektu • Struktura projektu: • Vygenerované základní stránky a UT
Model - perzistence • Demonstrace LINQ query: NerdDinnerDataContext db = new NerdDinnerDataContext(); Dinner dinner = db.Dinners.Single(d => d.DinnerID == 1); dinner.Title = "Changed Title"; dinner.Description = "This dinner will be fun"; db.SubmitChanges();
Model – perzistence lépe public class DinnerRepository { private NerdDinnerDataContext db = new NerdDinnerDataContext(); public IQueryable<Dinner> FindAllDinners() { return db.Dinners; }
Model – perzistence lépe public IQueryable<Dinner> FindUpcomingDinners() { return from dinner in db.Dinners where dinner.EventDate > DateTime.Now orderby dinner.EventDate select dinner; } public Dinner GetDinner(int id) { return db.Dinners.SingleOrDefault(d => d.DinnerID == id); }
Model – perzistence lépe public void Add(Dinner dinner) { db.Dinners.InsertOnSubmit(dinner); } public void Delete(Dinner dinner) { db.RSVPs.DeleteAllOnSubmit(dinner.RSVPs); db.Dinners.DeleteOnSubmit(dinner); } public void Save() { db.SubmitChanges(); }
Model - dotazy DinnerRepository dinnerRepository = new DinnerRepository(); Dinner dinner = dinnerRepository.GetDinner(5); dinner.Title = "Update Title"; dinner.HostedBy = "New Owner“; Dinner newDinner1 = new Dinner(); newDinner1.Title = "Dinner with Scott"; newDinner1.HostedBy = "ScotGu"; newDinner1.ContactPhone = "425-703-8072"; dinnerRepository.Save();
Přidání Controlleru public class DinnersController : Controller { // HTTP-GET: /Dinners/ public void Index(){ Response.Write("<h1>Coming Soon: Dinners</h1>"); } // HTTP-GET:/Dinners/Details/2 public void Details(int id){ Response.Write("<h1>Details DinnerID: " + id + </h1>"); } }
Routing public void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", //Route name "{controller}/{action}/{id}", // URL /params new { controller="Home", action="Index",id="" } // Param defaults ); }
Použití View z Controlleru public class DinnersController : Controller { DinnerRepository dinnerRepository = new DinnerRepository(); public ActionResult Index() { var dinners = dinnerRepository.FindUpcomingDinners().ToList(); return View("Index", dinners); } public ActionResult Details(int id) { Dinner dinner = dinnerRepository.GetDinner(id); if (dinner == null) return View("NotFound"); else return View("Details", dinner); } }
Použití View z Controlleru <asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server"> Details </asp:Content> <asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server"> <h2>Details</h2> <fieldset> <legend>Fields</legend> <p>DinnerID: <%=Html.Encode(Model.DinnerID) %></p> <p> Title: <%=Html.Encode(Model.Title) %></p>
Použití View z Controlleru <asp:Content ID="Title" ContentPlaceHolderID="TitleContent" runat="server„>Upcoming Dinners</asp:Content> <asp:Content ID="Main" ContentPlaceHolderID="MainContent" runat="server"> <h2>Upcoming Dinners</h2> <ul> <% foreach (var dinner in Model) { %> <li><%=Html.ActionLink(dinner.Title, "Details", new { id=dinner.DinnerID }) %> on <%=Html.Encode(dinner.EventDate.ToShortDateString())%> @ <%=Html.Encode(dinner.EventDate.ToShortTimeString())%> </li> <% } %> </ul> </asp:Content>
Update - POST [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, FormCollection formValues) { Dinner dinner = dinnerRepository.GetDinner(id); dinner.Title = Request.Form["Title"]; dinner.Description = Request.Form["Description"]; dinner.EventDate = DateTime.Parse(Request.Form["EventDate"]); dinner.Address = Request.Form["Address"]; dinner.Country = Request.Form["Country"]; dinner.ContactPhone = Request.Form["ContactPhone"]; dinnerRepository.Save(); return RedirectToAction("Details", new { id = dinner.DinnerID }); }
Update – POST lépe [AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id, FormCollection formValues) { Dinner dinner = dinnerRepository.GetDinner(id); UpdateModel(dinner); dinnerRepository.Save(); return RedirectToAction("Details", new { id = dinner.DinnerID }); }