1 / 32

Aplikacje sieciowe

Aplikacje sieciowe. MVC. Literatura. msdn.microsoft.com www.asp.net/mvc Screencast Pluralsight. Model Viewer Controler. Controler. Viewer. Model. Struktura Projektu MVC. Model. public class Osoba { [ Key ] public virtual int Id { get ; set ; } [ Required ]

kerryn
Télécharger la présentation

Aplikacje sieciowe

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Aplikacje sieciowe MVC

  2. Literatura • msdn.microsoft.com • www.asp.net/mvc • Screencast Pluralsight

  3. Model Viewer Controler Controler Viewer Model

  4. Struktura Projektu MVC

  5. Model publicclassOsoba { [Key] publicvirtualint Id { get; set; } [Required] [Display(Name = "Imię")] publicvirtualstring Imie { get; set; } [Required] publicvirtualstring Nazwisko { get; set; } [Display(Name = "Numer telefonu")] publicvirtualstring NrTelefonu { get; set; } [Display(Name = "Adres e-mail")] publicvirtualstring Email { get; set; } [Display(Name = "Numer dowodu")] publicvirtualstring NrDowodu { get; set; } ...} //

  6. publicclassStudent: Osoba { [Required] [Display(Name="Numer Indeksu")] publicvirtualstring NrIndeksu { get; set; } publicvirtualbool MaPraktyke { get; set; } publicvirtualDateTime Rocznik { get; set; } public Student() { Rocznik = DateTime.Now; } }

  7. Kontroler (Controler) publicclassStudentController : Controller { // // GET: /Student/ PraktykiDB baza = newPraktykiDB(); publicActionResult Index() { // BazaDanychPraktyk baza = new BazaDanychPraktyk(); var model = baza.Studenci; return View(model ); } ….

  8. Controler - Tworzenie publicActionResult NowyStudent() { var model = newStudent(); return View(model); } publicActionResult Create() { return NowyStudent(); } [HttpPost] publicActionResult Create(Student newStudent) { baza.Studenci.Add(newStudent); baza.SaveChanges(); return RedirectToAction("Index"); }

  9. Controler Edycja publicActionResult Edit(int id) { Student student = baza.Studenci.Single(s => s.Id == id); return View(student); } [HttpPost] publicActionResult Edit(int id, FormCollection collection) { Student student = baza.Studenci.Single(s => s.Id == id); if(TryUpdateModel(student)) { baza.SaveChanges(); return RedirectToAction("Index"); } return View(student); }

  10. Controler - Kasowanie

  11. Widok (View) • Index.cshml • Create.cshtml • Details.cshtml • Edit.cshtml • …

  12. Index.cshtml @model IEnumerable<PraktykiStudenckie.Models.Student> @{ ViewBag.Title = "Index"; } <h2>Index</h2> <p> @Html.ActionLink("Create New", "Create") </p> <table> <tr> <th> NrIndeksu </th> <th> Imie </th> <th> Nazwisko </th> … </tr> @foreach (var item in Model) { <tr> <td> @Html.DisplayFor(modelItem => item.NrIndeksu) </td> <td> @Html.DisplayFor(modelItem => item.Imie) </td> <td> @Html.DisplayFor(modelItem => item.Nazwisko) </td> …. <td> @Html.ActionLink("Edit", "Edit", new { id=item.Id }) | @Html.ActionLink("Details", "Details", new { id=item.Id }) | @Html.ActionLink("Delete", "Delete", new { /* id=item.PrimaryKey */ }) </td> </tr> } </table>

  13. Create.cshtml @model PraktykiStudenckie.Models.Student @{ ViewBag.Title = "Create"; } <h2>Create</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>Student</legend> <div class="editor-label"> @Html.LabelFor(model => model.NrIndeksu) </div> <div class="editor-field"> @Html.EditorFor(model => model.NrIndeksu) @Html.ValidationMessageFor(model => model.NrIndeksu) </div> <div class="editor-label"> @Html.LabelFor(model => model.Imie) </div> <div class="editor-field"> @Html.EditorFor(model => model.Imie) @Html.ValidationMessageFor(model => model.Imie) </div> <div class="editor-label"> @Html.LabelFor(model => model.Nazwisko) </div> <div class="editor-field"> @Html.EditorFor(model => model.Nazwisko) @Html.ValidationMessageFor(model => model.Nazwisko) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div>

  14. Html helpers • @Html.ActionLink • @Html.DisplayFor • @Html.LabelFor(model => model.NrIndeksu) • @Html.EditorFor(model => model.NrIndeksu) • @Html.ValidationMessageFor(model => model.NrIndeksu) • @Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js") • @Html.ActionLink • @Html.ValidationSummary(true) • Html.ValidationSummary(true) • Html.BeginForm()

  15. @Html.ActionLink("Treść linka", "Nazwa Akcji", new { /* id=Model.PrimaryKey */ }) • @Html.ActionLink("Dodaj Studenta", "Create", "Student") Parametry routingu Controller

  16. @Html.BeginForm @using (Html.BeginForm()) { <div> <fieldset> <legend>Account Information</legend> <div class="editor-label"> @Html.LabelFor(m => m.UserName) </div> <div class="editor-field"> @Html.TextBoxFor(m => m.UserName) @Html.ValidationMessageFor(m => m.UserName) </div> ………. <p> <input type="submit" value="Log On" /> </p> </fieldset> </div> }

  17. BeginForm - cd • BeginForm("Akcja", "Kontroler", …) • Określenie routingu, • Metody: Get, Post

  18. PartialView • Znacznik: <div id="logindisplay"> @Html.Partial("_LogOnPartial") </div> • Plik _LogOnPartia.cshtml @if(Request.IsAuthenticated) { <text>Welcome <strong>@User.Identity.Name</strong>! [ @Html.ActionLink("Log Off", "LogOff", "Account") ]</text> } else { @:[ @Html.ActionLink("Log On", "LogOn", "Account") ] }

  19. Dostęp do danych • DataBase First • Model First • Code First

  20. Inne Metody Tworzenie aplikacji MVC • Model Firsta) Definicja modelu – w entity frameweorkb) Tworzenie logiki biznesowej - generacja bazy danych • DataBase Firsta) Tworzenie bazy danychb) Generacja modelu EFc) Tworzenie logiki biznesowej

  21. Code First • Własne obiekty • Obiekty z Entity Framewerk

  22. Własne obiety publicclassBazaDanychPraktyk{ privateList<Student> studenci; publicList<Student> Studenci{ get { return studenci; } set { studenci = value; } } privateList<Umowa> umowy; publicList<Umowa> Umowy {get { return umowy; } set { umowy = value; } } privateList<Firma> firmy; publicList<Firma> Firmy { get { return firmy; } set { firmy = value; } } public BazaDanychPraktyk(){ studenci= newList<Student>(); firmy = newList<Firma>(); umowy= newList<Umowa>(); studenci.Add( newStudent(){Imie="Andrzej", Nazwisko="Nowak", Indeksu="3456"}); studenci.Add( newStudent(){Imie="Jan", Nazwisko="Kowalski",NrIndeksu="1234"}); firmy.Add(newFirma() { Nazwa = "Firma 1" }); firmy.Add(newFirma() { Nazwa = "Firma 2" }); firmy.Add(newFirma() { Nazwa = "Firma 3" }); umowy.Add(newUmowa() {Student= studenci[0], Firma=firmy[1],Id=1 }); } }

  23. Code First Definicja bazy publicclassPraktykiDB: DbContext { publicDbSet<Student> Studenci { get; set; } publicDbSet<Firma> Firmy { get; set; } publicDbSet<Umowa> Umowy { get; set; } } Web.config <configuration> <connectionStrings> <addname="PraktykiDB"connectionString="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|PraktykiDB.mdf;User Instance=true;Initial Catalog=PraktykiDB;MultipleActiveResultSets=true"providerName="System.Data.SqlClient" /> </connectionStrings>

  24. CodeFirst - Inicjalizacja Bazy classPraktykiDBInitializer : DropCreateDatabaseIfModelChanges<PraktykiStudenckie.Models.PraktykiDB>{ // DropCreateDatabaseAlways<PraktykiStudenckie.Models.PraktykiDB> protectedoverridevoid Seed(Models.PraktykiDB context) { base.Seed(context); context.Studenci.Add(newStudent(){Imie = "Andrzej",Nazwisko ="Nowak", ...} ); context.Studenci.Add(newStudent() {Imie = "Jan",Nazwisko ="Kowalski", ...} ); context.Firmy.Add(newFirma() { Nazwa = "Firma 1" }); context.Firmy.Add(newFirma() { Nazwa = "Firma 2" }); context.Firmy.Add(newFirma() { Nazwa = "Firma 3" }); context.SaveChanges(); context.Umowy.Add(newUmowa() { Student = context.Studenci.First(), Firma = context.Firmy.First() }); context.SaveChanges(); } }

  25. Global.asax protectedvoidApplication_Start() { AreaRegistration.RegisterAllAreas(); Database.SetInitializer(newPraktykiDBInitializer()); RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }

  26. Global.asax - cd publicstaticvoid RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(newHandleErrorAttribute()); } publicstaticvoid RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); }

  27. Atrybuty Formatujące i Walidujące • [Key] • [Display(Name = "Data Utworzenia")] • [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:d}")] • [Required] • [DataType(DataType.Date)] • [DataType(DataType.EmailAddress)] • [DataType(DataType.MultilineText)] • [DataType(DataType.Password)] • [DataType(DataType.PhoneNumber)]

  28. Inne Mechanizmy walidacji

More Related