mirror of
https://github.com/rudollee/LearningMVC.git
synced 2025-06-07 16:06:21 +00:00
Ch. 22 Templated Helper Methods
This commit is contained in:
parent
1eb39e5afc
commit
dabe7ab03e
@ -27,7 +27,8 @@ namespace HelperMethods.Controllers
|
||||
[HttpPost]
|
||||
public ActionResult CreatePerson(Person Person)
|
||||
{
|
||||
return View(Person);
|
||||
//return View(Person);
|
||||
return View("DisplayPerson", Person);
|
||||
}
|
||||
}
|
||||
}
|
@ -100,6 +100,10 @@
|
||||
<Content Include="Scripts\jquery-1.10.2.min.js" />
|
||||
<Content Include="Scripts\modernizr-2.6.2.js" />
|
||||
<Content Include="Web.config" />
|
||||
<Content Include="Views\Home\DisplayPerson.cshtml" />
|
||||
<Content Include="Views\Shared\EditorTemplates\Role.cshtml" />
|
||||
<Content Include="Views\Shared\EditorTemplates\Enum.cshtml" />
|
||||
<Content Include="Views\Shared\EditorTemplates\Boolean.cshtml" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="App_Start\RouteConfig.cs" />
|
||||
@ -108,6 +112,7 @@
|
||||
<DependentUpon>Global.asax</DependentUpon>
|
||||
</Compile>
|
||||
<Compile Include="Infrastructure\CustomHelpers.cs" />
|
||||
<Compile Include="Models\Metadata\PersonMetadata.cs" />
|
||||
<Compile Include="Models\Person.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
33
HelperMethods/Models/Metadata/PersonMetadata.cs
Normal file
33
HelperMethods/Models/Metadata/PersonMetadata.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
@ -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; }
|
||||
}
|
||||
|
@ -3,37 +3,53 @@
|
||||
@{
|
||||
ViewBag.Title = "CreatePerson";
|
||||
Layout = "/Views/Shared/_Layout.cshtml";
|
||||
Html.EnableClientValidation(false);
|
||||
}
|
||||
|
||||
<h2>CreatePerson</h2>
|
||||
<h2>CreatePerson @Html.LabelForModel() </h2>
|
||||
|
||||
@*@{ Html.BeginForm();}*@
|
||||
@using (Html.BeginRouteForm("FormRoute", new { }, FormMethod.Post,
|
||||
new { @class = "personClass", data_formtype = "person" }))
|
||||
new { @class = "personClass", data_formtype = "person" }))
|
||||
{
|
||||
|
||||
<div class="column">
|
||||
@Html.EditorForModel()
|
||||
</div>
|
||||
<div class="column">
|
||||
@Html.EditorFor(m => m.HomeAddress)
|
||||
</div>
|
||||
<!--
|
||||
<div class="dateElem">
|
||||
<label>PersonId</label>
|
||||
@*<input name="personId" value="@Model.PersonId" />*@
|
||||
@*@Html.TextBox("personId", @Model.PersonId)*@
|
||||
@Html.TextBoxFor(m => m.PersonId)
|
||||
@*@Html.TextBoxFor(m => m.PersonId)*@
|
||||
@* @Html.Editor("PersonId")*@
|
||||
</div>
|
||||
<div class="dateElem">
|
||||
<label>First Name</label>
|
||||
@*<input name="FirstName" value="@Model.FirstName" />*@
|
||||
@*@Html.TextBox("FirstName", @Model.FirstName)*@
|
||||
@Html.TextBoxFor(m => m.FirstName)
|
||||
@*@Html.TextBoxFor(m => m.FirstName)*@
|
||||
@* @Html.Editor("FirstName")*@
|
||||
</div>
|
||||
<div class="dateElem">
|
||||
<label>Last Name</label>
|
||||
@*<input name="LastName" value="@Model.LastName" />*@
|
||||
@*@Html.TextBox("LastName", @Model.LastName)*@
|
||||
@Html.TextBoxFor(m => m.LastName)
|
||||
@*@Html.TextBoxFor(m => m.LastName)*@
|
||||
@* @Html.EditorFor(m => m.LastName)*@
|
||||
</div>
|
||||
<div class="dateElem">
|
||||
<label>Role</label>
|
||||
@Html.DropDownListFor(m => m.Role, new SelectList(Enum.GetNames(typeof(HelperMethods.Models.Role))))
|
||||
@* @Html.DropDownListFor(m => m.Role, new SelectList(Enum.GetNames(typeof(HelperMethods.Models.Role))))*@
|
||||
@*@Html.EditorFor(m => m.Role)*@
|
||||
</div>
|
||||
<div class="dateElem">
|
||||
<label>Birth Date</label>
|
||||
@*@Html.EditorFor(m => m.BirthDate)*@
|
||||
</div>
|
||||
-->
|
||||
<input type="submit" value="Submit" />
|
||||
|
||||
}
|
||||
|
28
HelperMethods/Views/Home/DisplayPerson.cshtml
Normal file
28
HelperMethods/Views/Home/DisplayPerson.cshtml
Normal file
@ -0,0 +1,28 @@
|
||||
@model HelperMethods.Models.Person
|
||||
|
||||
@{
|
||||
ViewBag.Title = "DisplayPerson";
|
||||
Layout = "/Views/Shared/_Layout.cshtml";
|
||||
}
|
||||
|
||||
<h2>DisplayPerson</h2>
|
||||
<div class="dateElem">
|
||||
@Html.Label("PersonId")
|
||||
@Html.Display("PersonId")
|
||||
</div>
|
||||
<div class="dateElem">
|
||||
@Html.Label("FirstName")
|
||||
@Html.Display("FirstName")
|
||||
</div>
|
||||
<div class="dateElem">
|
||||
@Html.LabelFor(m => m.LastName)
|
||||
@Html.DisplayFor(m => m.LastName)
|
||||
</div>
|
||||
<div class="dateElem">
|
||||
@Html.LabelFor(m => m.Role)
|
||||
@Html.DisplayFor(m => m.Role)
|
||||
</div>
|
||||
<div class="dateElem">
|
||||
@Html.LabelFor(m => m.BirthDate)
|
||||
@Html.DisplayFor(m => m.BirthDate)
|
||||
</div>
|
15
HelperMethods/Views/Shared/EditorTemplates/Boolean.cshtml
Normal file
15
HelperMethods/Views/Shared/EditorTemplates/Boolean.cshtml
Normal file
@ -0,0 +1,15 @@
|
||||
@model bool?
|
||||
|
||||
@if (ViewData.ModelMetadata.IsNullableValueType && Model == null)
|
||||
{
|
||||
@:(True) (False) <b>(Not Set)</b>
|
||||
}
|
||||
else if (Model.Value)
|
||||
{
|
||||
@:<b>(True)</b> (False) (Not Set)
|
||||
}
|
||||
else
|
||||
{
|
||||
@:(True) <b>(False)</b> (Not Set)
|
||||
}
|
||||
|
14
HelperMethods/Views/Shared/EditorTemplates/Enum.cshtml
Normal file
14
HelperMethods/Views/Shared/EditorTemplates/Enum.cshtml
Normal file
@ -0,0 +1,14 @@
|
||||
@model Enum
|
||||
|
||||
@Html.DropDownListFor(m => m,
|
||||
Enum.GetValues(Model.GetType())
|
||||
.Cast<Enum>()
|
||||
.Select(m => {
|
||||
string enumVal = Enum.GetName(Model.GetTYpe()), m);
|
||||
return new SelectListItem()
|
||||
{
|
||||
Selected = (Model.ToString() == enumVal),
|
||||
Text = enumVal,
|
||||
Value = enumVal
|
||||
};
|
||||
})
|
4
HelperMethods/Views/Shared/EditorTemplates/Role.cshtml
Normal file
4
HelperMethods/Views/Shared/EditorTemplates/Role.cshtml
Normal file
@ -0,0 +1,4 @@
|
||||
@model HelperMethods.Models.Role
|
||||
|
||||
@Html.DropDownListFor(m => m, new SelectList(Enum.GetNames(Model.GetType()), Model.ToString()))
|
||||
|
@ -7,6 +7,13 @@
|
||||
<style type="text/css">
|
||||
label {display: inline-block; width: 100px;}
|
||||
div.dateElem {margin: 5px;}
|
||||
h2 > label { width: inherit;}
|
||||
.editor-label, .editor-field {float: left; margin-top: 10px;}
|
||||
.editor-field input { height: 20px; }
|
||||
.editor-label { clear: left;}
|
||||
.editor-field { margin-left: 10px; }
|
||||
input[type=submit] { float: left; clear: both; margin-top: 10px;}
|
||||
.column { float: left; margin: 10px;}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
Loading…
x
Reference in New Issue
Block a user