Aplikacje sieciowe

32
Aplikacje sieciowe MVC

description

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 ] - PowerPoint PPT Presentation

Transcript of Aplikacje sieciowe

Page 1: Aplikacje sieciowe

Aplikacje sieciowe

MVC

Page 2: Aplikacje sieciowe

Literatura

• msdn.microsoft.com• www.asp.net/mvc

– Screencast Pluralsight

Page 3: Aplikacje sieciowe

Model Viewer Controler

Controler

Viewer Model

Page 4: Aplikacje sieciowe
Page 5: Aplikacje sieciowe
Page 6: Aplikacje sieciowe

Struktura Projektu MVC

Page 7: Aplikacje sieciowe

Model public class Osoba { [Key] public virtual int Id { get; set; } [Required] [Display(Name = "Imię")] public virtual string Imie { get; set; } [Required] public virtual string Nazwisko { get; set; }

[Display(Name = "Numer telefonu")] public virtual string NrTelefonu { get; set; } [Display(Name = "Adres e-mail")] public virtual string Email { get; set; }

[Display(Name = "Numer dowodu")] public virtual string NrDowodu { get; set; }...}//

Page 8: Aplikacje sieciowe

public class Student: Osoba { [Required] [Display(Name="Numer Indeksu")]

public virtual string NrIndeksu { get; set; } public virtual bool MaPraktyke { get; set; } public virtual DateTime Rocznik { get; set; }

public Student() { Rocznik = DateTime.Now; } }

Page 9: Aplikacje sieciowe

Kontroler (Controler) public class StudentController : Controller { // // GET: /Student/ PraktykiDB baza = new PraktykiDB();

public ActionResult Index() { // BazaDanychPraktyk baza = new BazaDanychPraktyk();

var model = baza.Studenci; return View(model ); }….

Page 10: Aplikacje sieciowe

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

Page 11: Aplikacje sieciowe

Controler Edycja public ActionResult Edit(int id) { Student student = baza.Studenci.Single(s => s.Id == id); return View(student); } [HttpPost] public ActionResult 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); }

Page 12: Aplikacje sieciowe

Controler - Kasowanie

Page 13: Aplikacje sieciowe

Widok (View)

• Index.cshml• Create.cshtml• Details.cshtml• Edit.cshtml• …

Page 14: Aplikacje sieciowe

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>

Page 15: Aplikacje sieciowe

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>

Page 16: Aplikacje sieciowe

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()

Page 17: Aplikacje sieciowe

• @Html.ActionLink("Treść linka", "Nazwa Akcji", new { /* id=Model.PrimaryKey */ }

)

• @Html.ActionLink("Dodaj Studenta", "Create", "Student")

Parametry routingu

Controller

Page 18: Aplikacje sieciowe

@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>}

Page 19: Aplikacje sieciowe

BeginForm - cd

• BeginForm("Akcja", "Kontroler", …)– Określenie routingu,– Metody: Get, Post

Page 20: Aplikacje sieciowe

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") ]}

Page 21: Aplikacje sieciowe

Dostęp do danych

• DataBase First• Model First• Code First

Page 22: Aplikacje sieciowe

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

Page 23: Aplikacje sieciowe

Code First

• Własne obiekty• Obiekty z Entity Framewerk

Page 24: Aplikacje sieciowe

Własne obiety public class BazaDanychPraktyk{ private List<Student> studenci; public List<Student> Studenci{ get { return studenci; } set { studenci = value; } } private List<Umowa> umowy; public List<Umowa> Umowy {get { return umowy; } set { umowy = value; } } private List<Firma> firmy; public List<Firma> Firmy { get { return firmy; } set { firmy = value; } } public BazaDanychPraktyk(){ studenci= new List<Student>(); firmy = new List<Firma>();

umowy= new List<Umowa>(); studenci.Add( new Student(){Imie="Andrzej", Nazwisko="Nowak", Indeksu="3456"}); studenci.Add( new Student(){Imie="Jan", Nazwisko="Kowalski",NrIndeksu="1234"}); firmy.Add(new Firma() { Nazwa = "Firma 1" }); firmy.Add(new Firma() { Nazwa = "Firma 2" }); firmy.Add(new Firma() { Nazwa = "Firma 3" });

umowy.Add(new Umowa() {Student= studenci[0], Firma=firmy[1],Id=1 }); } }

Page 25: Aplikacje sieciowe

Code FirstDefinicja bazypublic class PraktykiDB: DbContext { public DbSet<Student> Studenci { get; set; } public DbSet<Firma> Firmy { get; set; } public DbSet<Umowa> Umowy { get; set; } }Web.config<configuration> <connectionStrings><add name="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>

Page 26: Aplikacje sieciowe

CodeFirst - Inicjalizacja Bazy class PraktykiDBInitializer : DropCreateDatabaseIfModelChanges<PraktykiStudenckie.Models.PraktykiDB>{ // DropCreateDatabaseAlways<PraktykiStudenckie.Models.PraktykiDB> protected override void Seed(Models.PraktykiDB context) { base.Seed(context); context.Studenci.Add(new Student(){Imie = "Andrzej",Nazwisko ="Nowak", ...} ); context.Studenci.Add(new Student() {Imie = "Jan",Nazwisko ="Kowalski", ...} );

context.Firmy.Add(new Firma() { Nazwa = "Firma 1" }); context.Firmy.Add(new Firma() { Nazwa = "Firma 2" }); context.Firmy.Add(new Firma() { Nazwa = "Firma 3" });

context.SaveChanges();

context.Umowy.Add(new Umowa() { Student = context.Studenci.First(), Firma = context.Firmy.First() });

context.SaveChanges(); } }

Page 27: Aplikacje sieciowe

Global.asaxprotected void Application_Start() { AreaRegistration.RegisterAllAreas(); Database.SetInitializer(new PraktykiDBInitializer());

RegisterGlobalFilters(GlobalFilters.Filters); RegisterRoutes(RouteTable.Routes); }

Page 28: Aplikacje sieciowe

Global.asax - cd public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); }

public static void 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 );

}

Page 29: Aplikacje sieciowe

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)]

Page 30: Aplikacje sieciowe

Inne Mechanizmy walidacji

Page 31: Aplikacje sieciowe
Page 32: Aplikacje sieciowe