From dabe7ab03e7a53c99e433e62fada4211d91c56b7 Mon Sep 17 00:00:00 2001 From: wook Date: Tue, 18 Apr 2017 01:52:16 +0900 Subject: [PATCH] Ch. 22 Templated Helper Methods --- HelperMethods/Controllers/HomeController.cs | 3 +- HelperMethods/HelperMethods.csproj | 5 +++ .../Models/Metadata/PersonMetadata.cs | 33 +++++++++++++++++++ HelperMethods/Models/Person.cs | 17 +++++++++- HelperMethods/Views/Home/CreatePerson.cshtml | 30 +++++++++++++---- HelperMethods/Views/Home/DisplayPerson.cshtml | 28 ++++++++++++++++ .../Shared/EditorTemplates/Boolean.cshtml | 15 +++++++++ .../Views/Shared/EditorTemplates/Enum.cshtml | 14 ++++++++ .../Views/Shared/EditorTemplates/Role.cshtml | 4 +++ HelperMethods/Views/Shared/_Layout.cshtml | 7 ++++ 10 files changed, 147 insertions(+), 9 deletions(-) create mode 100644 HelperMethods/Models/Metadata/PersonMetadata.cs create mode 100644 HelperMethods/Views/Home/DisplayPerson.cshtml create mode 100644 HelperMethods/Views/Shared/EditorTemplates/Boolean.cshtml create mode 100644 HelperMethods/Views/Shared/EditorTemplates/Enum.cshtml create mode 100644 HelperMethods/Views/Shared/EditorTemplates/Role.cshtml diff --git a/HelperMethods/Controllers/HomeController.cs b/HelperMethods/Controllers/HomeController.cs index 2a56111..0d6dd76 100644 --- a/HelperMethods/Controllers/HomeController.cs +++ b/HelperMethods/Controllers/HomeController.cs @@ -27,7 +27,8 @@ namespace HelperMethods.Controllers [HttpPost] public ActionResult CreatePerson(Person Person) { - return View(Person); + //return View(Person); + return View("DisplayPerson", Person); } } } \ No newline at end of file diff --git a/HelperMethods/HelperMethods.csproj b/HelperMethods/HelperMethods.csproj index be9e432..b3f9c76 100644 --- a/HelperMethods/HelperMethods.csproj +++ b/HelperMethods/HelperMethods.csproj @@ -100,6 +100,10 @@ + + + + @@ -108,6 +112,7 @@ Global.asax + diff --git a/HelperMethods/Models/Metadata/PersonMetadata.cs b/HelperMethods/Models/Metadata/PersonMetadata.cs new file mode 100644 index 0000000..fa8d72b --- /dev/null +++ b/HelperMethods/Models/Metadata/PersonMetadata.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using System.ComponentModel; +using System.ComponentModel.DataAnnotations; + +namespace HelperMethods.Models +{ + [DisplayName("New Person")] + public partial class PersonMetadata + { + [HiddenInput(DisplayValue = false)] + public int PersonId { get; set; } + + [Display(Name ="First")] + public string FirstName { get; set; } + + [Display(Name = "Last")] + public string LastName { get; set; } + + [Display(Name = "Birth Date")] + [DataType(DataType.Date)] + public DateTime BirthDate { get; set; } + + [Display(Name = "Approved")] + public bool IsApproved { get; set; } + + [UIHint("Enum")] + public Role Role { get; set; } + } +} \ No newline at end of file diff --git a/HelperMethods/Models/Person.cs b/HelperMethods/Models/Person.cs index f6569c8..09c4cce 100644 --- a/HelperMethods/Models/Person.cs +++ b/HelperMethods/Models/Person.cs @@ -2,16 +2,31 @@ using System.Collections.Generic; using System.Linq; using System.Web; +using System.Web.Mvc; +using System.ComponentModel.DataAnnotations; +using System.ComponentModel; namespace HelperMethods.Models { - public class Person + [DisplayName("New Person")] + public partial class Person { + [HiddenInput(DisplayValue = false)] public int PersonId { get; set; } + + [Display(Name ="First")] public string FirstName { get; set; } + + [Display(Name ="Last")] public string LastName { get; set; } + + [Display(Name = "Birth Date")] + [DataType(DataType.Date)] public DateTime BirthDate { get; set; } + public Address HomeAddress { get; set; } + + [Display(Name ="Approved")] public bool IsApproved { get; set; } public Role Role { get; set; } } diff --git a/HelperMethods/Views/Home/CreatePerson.cshtml b/HelperMethods/Views/Home/CreatePerson.cshtml index d3eccfb..e4dda28 100644 --- a/HelperMethods/Views/Home/CreatePerson.cshtml +++ b/HelperMethods/Views/Home/CreatePerson.cshtml @@ -3,37 +3,53 @@ @{ ViewBag.Title = "CreatePerson"; Layout = "/Views/Shared/_Layout.cshtml"; + Html.EnableClientValidation(false); } -

CreatePerson

+

CreatePerson @Html.LabelForModel()

@*@{ Html.BeginForm();}*@ @using (Html.BeginRouteForm("FormRoute", new { }, FormMethod.Post, - new { @class = "personClass", data_formtype = "person" })) + new { @class = "personClass", data_formtype = "person" })) { - +
+ @Html.EditorForModel() +
+
+ @Html.EditorFor(m => m.HomeAddress) +
+ } diff --git a/HelperMethods/Views/Home/DisplayPerson.cshtml b/HelperMethods/Views/Home/DisplayPerson.cshtml new file mode 100644 index 0000000..9e8fa7e --- /dev/null +++ b/HelperMethods/Views/Home/DisplayPerson.cshtml @@ -0,0 +1,28 @@ +@model HelperMethods.Models.Person + +@{ + ViewBag.Title = "DisplayPerson"; + Layout = "/Views/Shared/_Layout.cshtml"; +} + +

DisplayPerson

+
+ @Html.Label("PersonId") + @Html.Display("PersonId") +
+
+ @Html.Label("FirstName") + @Html.Display("FirstName") +
+
+ @Html.LabelFor(m => m.LastName) + @Html.DisplayFor(m => m.LastName) +
+
+ @Html.LabelFor(m => m.Role) + @Html.DisplayFor(m => m.Role) +
+
+ @Html.LabelFor(m => m.BirthDate) + @Html.DisplayFor(m => m.BirthDate) +
diff --git a/HelperMethods/Views/Shared/EditorTemplates/Boolean.cshtml b/HelperMethods/Views/Shared/EditorTemplates/Boolean.cshtml new file mode 100644 index 0000000..bb2dad6 --- /dev/null +++ b/HelperMethods/Views/Shared/EditorTemplates/Boolean.cshtml @@ -0,0 +1,15 @@ +@model bool? + +@if (ViewData.ModelMetadata.IsNullableValueType && Model == null) +{ + @:(True) (False) (Not Set) +} +else if (Model.Value) +{ + @:(True) (False) (Not Set) +} +else +{ + @:(True) (False) (Not Set) +} + diff --git a/HelperMethods/Views/Shared/EditorTemplates/Enum.cshtml b/HelperMethods/Views/Shared/EditorTemplates/Enum.cshtml new file mode 100644 index 0000000..6f4bfd4 --- /dev/null +++ b/HelperMethods/Views/Shared/EditorTemplates/Enum.cshtml @@ -0,0 +1,14 @@ +@model Enum + +@Html.DropDownListFor(m => m, + Enum.GetValues(Model.GetType()) + .Cast() + .Select(m => { + string enumVal = Enum.GetName(Model.GetTYpe()), m); + return new SelectListItem() + { + Selected = (Model.ToString() == enumVal), + Text = enumVal, + Value = enumVal + }; + }) diff --git a/HelperMethods/Views/Shared/EditorTemplates/Role.cshtml b/HelperMethods/Views/Shared/EditorTemplates/Role.cshtml new file mode 100644 index 0000000..027a2e9 --- /dev/null +++ b/HelperMethods/Views/Shared/EditorTemplates/Role.cshtml @@ -0,0 +1,4 @@ +@model HelperMethods.Models.Role + +@Html.DropDownListFor(m => m, new SelectList(Enum.GetNames(Model.GetType()), Model.ToString())) + diff --git a/HelperMethods/Views/Shared/_Layout.cshtml b/HelperMethods/Views/Shared/_Layout.cshtml index 07caacc..0314cd7 100644 --- a/HelperMethods/Views/Shared/_Layout.cshtml +++ b/HelperMethods/Views/Shared/_Layout.cshtml @@ -7,6 +7,13 @@