Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Commit 93de171

Browse files
dougbuNTaylorMullen
authored andcommitted
Add TagHelperSample.Web
1 parent 448ac5a commit 93de171

File tree

9 files changed

+348
-0
lines changed

9 files changed

+348
-0
lines changed

Mvc.sln

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "WebApiCompatShimWebSite", "
9898
EndProject
9999
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "Microsoft.AspNet.Mvc.WebApiCompatShimTest", "test\Microsoft.AspNet.Mvc.WebApiCompatShimTest\Microsoft.AspNet.Mvc.WebApiCompatShimTest.kproj", "{5DE8E4D9-AACD-4B5F-819F-F091383FB996}"
100100
EndProject
101+
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TagHelperSample.Web", "samples\TagHelperSample.Web\TagHelperSample.Web.kproj", "{2223120F-D675-40DA-8CD8-11DC14A0B2C7}"
102+
EndProject
101103
Global
102104
GlobalSection(SolutionConfigurationPlatforms) = preSolution
103105
Debug|Any CPU = Debug|Any CPU
@@ -518,6 +520,16 @@ Global
518520
{5DE8E4D9-AACD-4B5F-819F-F091383FB996}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
519521
{5DE8E4D9-AACD-4B5F-819F-F091383FB996}.Release|Mixed Platforms.Build.0 = Release|Any CPU
520522
{5DE8E4D9-AACD-4B5F-819F-F091383FB996}.Release|x86.ActiveCfg = Release|Any CPU
523+
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
524+
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
525+
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
526+
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
527+
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Debug|x86.ActiveCfg = Debug|Any CPU
528+
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
529+
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|Any CPU.Build.0 = Release|Any CPU
530+
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
531+
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|Mixed Platforms.Build.0 = Release|Any CPU
532+
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|x86.ActiveCfg = Release|Any CPU
521533
EndGlobalSection
522534
GlobalSection(SolutionProperties) = preSolution
523535
HideSolutionNode = FALSE
@@ -565,5 +577,6 @@ Global
565577
{23D30B8C-04B1-4577-A604-ED27EA1E4A0E} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
566578
{B2B7BC91-688E-4C1E-A71F-CE948D958DDF} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
567579
{5DE8E4D9-AACD-4B5F-819F-F091383FB996} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1}
580+
{2223120F-D675-40DA-8CD8-11DC14A0B2C7} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
568581
EndGlobalSection
569582
EndGlobal
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+

2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using Microsoft.AspNet.Mvc;
5+
using Microsoft.AspNet.Mvc.Rendering;
6+
using TagHelperSample.Web.Models;
7+
8+
namespace TagHelperSample.Web.Controllers
9+
{
10+
public class HomeController : Controller
11+
{
12+
private static readonly IEnumerable<SelectListItem> _items = new SelectList(Enumerable.Range(7, 13));
13+
private static readonly Dictionary<int, User> _users = new Dictionary<int, User>();
14+
private static int _next;
15+
16+
public HomeController()
17+
{
18+
// Unable to set ViewBag from constructor. Does this work in MVC 5.2?
19+
////ViewBag.Items = _items;
20+
}
21+
22+
// GET: /<controller>/
23+
public IActionResult Index()
24+
{
25+
return View(_users.Values);
26+
}
27+
28+
// GET: /Home/Create
29+
public IActionResult Create()
30+
{
31+
ViewBag.Items = _items;
32+
return View();
33+
}
34+
35+
// POST: Home/Create
36+
[HttpPost]
37+
public IActionResult Create(User user)
38+
{
39+
if (user != null && ModelState.IsValid)
40+
{
41+
var id = _next++;
42+
user.Id = id;
43+
_users[id] = user;
44+
return RedirectToAction("Index");
45+
}
46+
47+
ViewBag.Items = _items;
48+
return View();
49+
}
50+
51+
// GET: /Home/Edit/5
52+
public IActionResult Edit(int id)
53+
{
54+
User user;
55+
_users.TryGetValue(id, out user);
56+
57+
ViewBag.Items = _items;
58+
return View(user);
59+
}
60+
61+
// POST: Home/Edit/5
62+
[HttpPost]
63+
public IActionResult Edit(int id, User user)
64+
{
65+
if (user != null && id == user.Id && _users.ContainsKey(id) && ModelState.IsValid)
66+
{
67+
_users[id] = user;
68+
return RedirectToAction("Index");
69+
}
70+
71+
ViewBag.Items = _items;
72+
return View();
73+
}
74+
}
75+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+

2+
using System;
3+
4+
namespace TagHelperSample.Web.Models
5+
{
6+
public class User
7+
{
8+
public int Id { get; set; }
9+
10+
public string Name { get; set; }
11+
12+
public string Blurb { get; set; }
13+
14+
public DateTimeOffset DateOfBirth { get; set; }
15+
16+
public int YearsEmployeed { get; set; }
17+
}
18+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+

2+
using Microsoft.AspNet.Builder;
3+
using Microsoft.Framework.DependencyInjection;
4+
5+
namespace TagHelperSample.Web
6+
{
7+
public class Startup
8+
{
9+
public void Configure(IApplicationBuilder app)
10+
{
11+
app.UseServices(services => services.AddMvc());
12+
app.UseMvc();
13+
}
14+
}
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<PropertyGroup>
4+
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
5+
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
6+
</PropertyGroup>
7+
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
8+
<PropertyGroup Label="Globals">
9+
<ProjectGuid>2223120f-d675-40da-8cd8-11dc14a0b2c7</ProjectGuid>
10+
<OutputType>Web</OutputType>
11+
<RootNamespace>TagHelperSample.Web</RootNamespace>
12+
</PropertyGroup>
13+
<PropertyGroup>
14+
<SchemaVersion>2.0</SchemaVersion>
15+
<DevelopmentServerPort>31726</DevelopmentServerPort>
16+
</PropertyGroup>
17+
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
18+
</Project>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
2+
@using TagHelperSample.Web.Models
3+
@model User
4+
5+
<h2>Create</h2>
6+
7+
@* anti-forgery is on by default *@
8+
@* form will special-case anything that looks like a URI i.e. contains a '/' or doesn't match an action *@
9+
<form anti-forgery="false" action="Create">
10+
<div class="form-horizontal">
11+
@* validation summary tag helper will target just <div/> elements and append the list of errors *@
12+
@* - i.e. this helper, like <select/> helper has ContentBehavior.Append *@
13+
@* validation-model-errors-only="true" implies validation-summary="true" *@
14+
@* helper does nothing if model is valid and (client-side validation is disabled or validation-model-errors-only="true") *@
15+
@* don't need a bound attribute to match Html.ValidationSummary()'s headerTag parameter; users wrap message as they wish *@
16+
@* initially at least, will not remove the <div/> if list isn't generated *@
17+
@* - should helper remove the <div/> if list isn't generated? *@
18+
@* - (Html.ValidationSummary returns empty string despite non-empty message parameter) *@
19+
<div validation-summary="true" validation-model-errors-only="true">
20+
<span style="color:red">This is my message</span>
21+
</div>
22+
23+
@* element will have correct name and id attributes for Id property. unusual part is the constant value. *@
24+
@* - the helper will _not_ override the user-specified "value" attribute *@
25+
<input type="hidden" for="Id" value="0" />
26+
27+
<div class="form-group">
28+
@* no special-case for the "for" attribute; may eventually need to opt out on per-element basis here and in <form/> *@
29+
<label for="Name" class="control-label col-md-2" style="color:blue" />
30+
<div class="col-md-10">
31+
<input type="text" for="Name" style="color:blue" />
32+
<span validation-for="Name" style="color:blue" />
33+
</div>
34+
</div>
35+
<div class="form-group">
36+
<label for="DateOfBirth" class="control-label col-md-2" />
37+
<div class="col-md-10">
38+
@* will automatically infer type="date" (reused HTML attribute) and format="{0:d}" (optional bound attribute) *@
39+
<input for="DateOfBirth" />
40+
<span validation-for="DateOfBirth">How old are you?</span>
41+
</div>
42+
</div>
43+
<div class="form-group">
44+
<label for="YearsEmployeed" class="control-label col-md-2" />
45+
<div class="col-md-10">
46+
@* <select/> tag helper has ContentBehavior.Append -- items render after static options *@
47+
<select for="YearsEmployeed" items="(IEnumerable<SelectListItem>)ViewBag.Items" size="2" class="form-control">
48+
@* schedule-wise option tag helper (which adds "selected" attribute to static <option/>s) comes after helpers *@
49+
@* - static use of "selected" attribute may cause HTML errors if in a single-selection <select/> *@
50+
@* - @NTaylorMullen thinks <option/> tag helper could tell <select/> helper not to select anything from "items" *@
51+
@* - wouldn't help if user selected one static <option/> and expression indicated another, especially one earlier in the <select/> *@
52+
@* - may need a "default" bound parameter on the <select/> to avoid these cases and maintain "don't override" *@
53+
<option value="" selected="selected">Why didn't you select anything?</option>
54+
<optgroup label="Newby">
55+
<option value="0">Less than 1</option>
56+
<option value="1">1</option>
57+
</optgroup>
58+
<option value="2">2</option>
59+
<option value="3">3</option>
60+
<option value="4">4</option>
61+
<option value="5">5</option>
62+
<option value="6">6</option>
63+
</select>
64+
65+
@* targets only <span/> in Beta; does not support equivalent of Html.ValidationMessageFor()'s tag parameter *@
66+
@* - may eventually either support additional tags e.g. <p/> and <div/> or all tags /> *@
67+
<span validation-for="YearsEmployeed" />
68+
</div>
69+
</div>
70+
<div class="form-group">
71+
<label for="Blurb" class="control-label col-md-2" />
72+
<div class="col-md-10">
73+
<textarea rows="4" for="Blurb"></textarea>
74+
<span validation-for="Blurb" />
75+
</div>
76+
</div>
77+
78+
<div class="form-group">
79+
<div class="col-md-offset-2 col-md-10">
80+
@* this <input/> lacks a "for" attribute and will not be changed by the <input/> tag helper *@
81+
<input type="submit" value="Create" class="btn btn-default" />
82+
</div>
83+
</div>
84+
</div>
85+
</form>
86+
87+
<div>
88+
<a action="Index">Back to list</a>
89+
</div>
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2+
@using TagHelperSample.Web.Models
3+
@model User
4+
5+
<h2>Edit</h2>
6+
7+
<form>
8+
<div class="form-horizontal">
9+
<div validation-summary="true"/>
10+
<input type="hidden" for="Id" />
11+
12+
<div class="form-group">
13+
<label for="Name" class="control-label col-md-2" />
14+
<div class="col-md-10">
15+
<input type="text" for="Name" />
16+
<span validation-for="Name" />
17+
</div>
18+
</div>
19+
<div class="form-group">
20+
<label for="DateOfBirth" class="control-label col-md-2" />
21+
<div class="col-md-10">
22+
<input type="date" for="DateOfBirth" format="{0:d}" />
23+
<span validation-for="DateOfBirth">How old are you?</span>
24+
</div>
25+
</div>
26+
<div class="form-group">
27+
<label for="YearsEmployeed" class="control-label col-md-2" />
28+
<div class="col-md-10">
29+
<select for="YearsEmployeed" items="(IEnumerable<SelectListItem>)ViewBag.Items" size="2" class="form-control">
30+
<optgroup label="Newby">
31+
<option value="0">Less than 1</option>
32+
<option value="1">1</option>
33+
</optgroup>
34+
<option value="2">2</option>
35+
<option value="3">3</option>
36+
<option value="4">4</option>
37+
<option value="5">5</option>
38+
<option value="6">6</option>
39+
</select>
40+
<span validation-for="YearsEmployeed" />
41+
</div>
42+
</div>
43+
<div class="form-group">
44+
<label for="Blurb" class="control-label col-md-2" />
45+
<div class="col-md-10">
46+
<textarea rows="4" for="Blurb"></textarea>
47+
<span validation-for="Blurb" />
48+
</div>
49+
</div>
50+
51+
<div class="form-group">
52+
<div class="col-md-offset-2 col-md-10">
53+
<input type="submit" value="Save" class="btn btn-default" />
54+
</div>
55+
</div>
56+
</div>
57+
</form>
58+
59+
<div>
60+
<a action="Index">Back to list</a>
61+
</div>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
2+
@using TagHelperSample.Web.Models
3+
@model IEnumerable<User>
4+
5+
<h2>Index</h2>
6+
<p>
7+
<a action="Create">Create New</a>
8+
</p>
9+
10+
@if (Model != null && Model.Count() != 0)
11+
{
12+
<div class="form-horizontal">
13+
@foreach (var item in Model)
14+
{
15+
<div class="form-group">
16+
<label for="@item.Name" />
17+
<input type="text" for="@item.Name" disabled="disabled" readonly="readonly" />
18+
</div>
19+
<div class="form-group">
20+
<label for="@item.DateOfBirth" />
21+
<input type="date" for="@item.DateOfBirth" disabled="disabled" readonly="readonly" />
22+
</div>
23+
<div class="form-group">
24+
<label for="@item.YearsEmployeed" />
25+
<input type="number" for="@item.YearsEmployeed" disabled="disabled" readonly="readonly" />
26+
</div>
27+
<div class="form-group">
28+
<label for="@item.Blurb" />
29+
<textarea rows="4" for="@item.Blurb" disabled="disabled" readonly="readonly" />
30+
</div>
31+
32+
<a action="Edit" controller="Home" route="MyRouteName" route-id="@item.Id">Edit</a>
33+
}
34+
</div>
35+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"compilationOptions": {
3+
"warningsAsErrors": true
4+
},
5+
"dependencies": {
6+
"Microsoft.AspNet.Mvc": "6.0.0-*",
7+
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
8+
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
9+
"Microsoft.Framework.ConfigurationModel": "1.0.0-*"
10+
},
11+
"commands": {
12+
"web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001"
13+
},
14+
"frameworks": {
15+
"aspnet50": {
16+
"dependencies": {
17+
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*"
18+
}
19+
},
20+
"aspnetcore50": {
21+
"dependencies": { }
22+
}
23+
}
24+
}

0 commit comments

Comments
 (0)