Add TagHelperSample.Web

This commit is contained in:
Doug Bunting 2014-10-07 12:15:52 -07:00
parent 9fadcf74ca
commit 659e759de2
9 changed files with 348 additions and 0 deletions

13
Mvc.sln
View File

@ -98,6 +98,8 @@ Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "WebApiCompatShimWebSite", "
EndProject
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}"
EndProject
Project("{8BB2217D-0F2D-49D1-97BC-3654ED321F3B}") = "TagHelperSample.Web", "samples\TagHelperSample.Web\TagHelperSample.Web.kproj", "{2223120F-D675-40DA-8CD8-11DC14A0B2C7}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -518,6 +520,16 @@ Global
{5DE8E4D9-AACD-4B5F-819F-F091383FB996}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{5DE8E4D9-AACD-4B5F-819F-F091383FB996}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{5DE8E4D9-AACD-4B5F-819F-F091383FB996}.Release|x86.ActiveCfg = Release|Any CPU
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Debug|Any CPU.Build.0 = Debug|Any CPU
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Debug|x86.ActiveCfg = Debug|Any CPU
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|Any CPU.ActiveCfg = Release|Any CPU
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|Any CPU.Build.0 = Release|Any CPU
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|Mixed Platforms.ActiveCfg = Release|Any CPU
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|Mixed Platforms.Build.0 = Release|Any CPU
{2223120F-D675-40DA-8CD8-11DC14A0B2C7}.Release|x86.ActiveCfg = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -565,5 +577,6 @@ Global
{23D30B8C-04B1-4577-A604-ED27EA1E4A0E} = {32285FA4-6B46-4D6B-A840-2B13E4C8B58E}
{B2B7BC91-688E-4C1E-A71F-CE948D958DDF} = {16703B76-C9F7-4C75-AE6C-53D92E308E3C}
{5DE8E4D9-AACD-4B5F-819F-F091383FB996} = {3BA657BF-28B1-42DA-B5B0-1C4601FCF7B1}
{2223120F-D675-40DA-8CD8-11DC14A0B2C7} = {DAAE4C74-D06F-4874-A166-33305D2643CE}
EndGlobalSection
EndGlobal

View File

@ -0,0 +1,75 @@

using System.Collections.Generic;
using System.Linq;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.Rendering;
using TagHelperSample.Web.Models;
namespace TagHelperSample.Web.Controllers
{
public class HomeController : Controller
{
private static readonly IEnumerable<SelectListItem> _items = new SelectList(Enumerable.Range(7, 13));
private static readonly Dictionary<int, User> _users = new Dictionary<int, User>();
private static int _next;
public HomeController()
{
// Unable to set ViewBag from constructor. Does this work in MVC 5.2?
////ViewBag.Items = _items;
}
// GET: /<controller>/
public IActionResult Index()
{
return View(_users.Values);
}
// GET: /Home/Create
public IActionResult Create()
{
ViewBag.Items = _items;
return View();
}
// POST: Home/Create
[HttpPost]
public IActionResult Create(User user)
{
if (user != null && ModelState.IsValid)
{
var id = _next++;
user.Id = id;
_users[id] = user;
return RedirectToAction("Index");
}
ViewBag.Items = _items;
return View();
}
// GET: /Home/Edit/5
public IActionResult Edit(int id)
{
User user;
_users.TryGetValue(id, out user);
ViewBag.Items = _items;
return View(user);
}
// POST: Home/Edit/5
[HttpPost]
public IActionResult Edit(int id, User user)
{
if (user != null && id == user.Id && _users.ContainsKey(id) && ModelState.IsValid)
{
_users[id] = user;
return RedirectToAction("Index");
}
ViewBag.Items = _items;
return View();
}
}
}

View File

@ -0,0 +1,18 @@

using System;
namespace TagHelperSample.Web.Models
{
public class User
{
public int Id { get; set; }
public string Name { get; set; }
public string Blurb { get; set; }
public DateTimeOffset DateOfBirth { get; set; }
public int YearsEmployeed { get; set; }
}
}

View File

@ -0,0 +1,15 @@

using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;
namespace TagHelperSample.Web
{
public class Startup
{
public void Configure(IApplicationBuilder app)
{
app.UseServices(services => services.AddMvc());
app.UseMvc();
}
}
}

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
<PropertyGroup Label="Globals">
<ProjectGuid>2223120f-d675-40da-8cd8-11dc14a0b2c7</ProjectGuid>
<OutputType>Web</OutputType>
<RootNamespace>TagHelperSample.Web</RootNamespace>
</PropertyGroup>
<PropertyGroup>
<SchemaVersion>2.0</SchemaVersion>
<DevelopmentServerPort>31726</DevelopmentServerPort>
</PropertyGroup>
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
</Project>

View File

@ -0,0 +1,89 @@

@using TagHelperSample.Web.Models
@model User
<h2>Create</h2>
@* anti-forgery is on by default *@
@* form will special-case anything that looks like a URI i.e. contains a '/' or doesn't match an action *@
<form anti-forgery="false" action="Create">
<div class="form-horizontal">
@* validation summary tag helper will target just <div/> elements and append the list of errors *@
@* - i.e. this helper, like <select/> helper has ContentBehavior.Append *@
@* validation-model-errors-only="true" implies validation-summary="true" *@
@* helper does nothing if model is valid and (client-side validation is disabled or validation-model-errors-only="true") *@
@* don't need a bound attribute to match Html.ValidationSummary()'s headerTag parameter; users wrap message as they wish *@
@* initially at least, will not remove the <div/> if list isn't generated *@
@* - should helper remove the <div/> if list isn't generated? *@
@* - (Html.ValidationSummary returns empty string despite non-empty message parameter) *@
<div validation-summary="true" validation-model-errors-only="true">
<span style="color:red">This is my message</span>
</div>
@* element will have correct name and id attributes for Id property. unusual part is the constant value. *@
@* - the helper will _not_ override the user-specified "value" attribute *@
<input type="hidden" for="Id" value="0" />
<div class="form-group">
@* no special-case for the "for" attribute; may eventually need to opt out on per-element basis here and in <form/> *@
<label for="Name" class="control-label col-md-2" style="color:blue" />
<div class="col-md-10">
<input type="text" for="Name" style="color:blue" />
<span validation-for="Name" style="color:blue" />
</div>
</div>
<div class="form-group">
<label for="DateOfBirth" class="control-label col-md-2" />
<div class="col-md-10">
@* will automatically infer type="date" (reused HTML attribute) and format="{0:d}" (optional bound attribute) *@
<input for="DateOfBirth" />
<span validation-for="DateOfBirth">How old are you?</span>
</div>
</div>
<div class="form-group">
<label for="YearsEmployeed" class="control-label col-md-2" />
<div class="col-md-10">
@* <select/> tag helper has ContentBehavior.Append -- items render after static options *@
<select for="YearsEmployeed" items="(IEnumerable<SelectListItem>)ViewBag.Items" size="2" class="form-control">
@* schedule-wise option tag helper (which adds "selected" attribute to static <option/>s) comes after helpers *@
@* - static use of "selected" attribute may cause HTML errors if in a single-selection <select/> *@
@* - @NTaylorMullen thinks <option/> tag helper could tell <select/> helper not to select anything from "items" *@
@* - wouldn't help if user selected one static <option/> and expression indicated another, especially one earlier in the <select/> *@
@* - may need a "default" bound parameter on the <select/> to avoid these cases and maintain "don't override" *@
<option value="" selected="selected">Why didn't you select anything?</option>
<optgroup label="Newby">
<option value="0">Less than 1</option>
<option value="1">1</option>
</optgroup>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
@* targets only <span/> in Beta; does not support equivalent of Html.ValidationMessageFor()'s tag parameter *@
@* - may eventually either support additional tags e.g. <p/> and <div/> or all tags /> *@
<span validation-for="YearsEmployeed" />
</div>
</div>
<div class="form-group">
<label for="Blurb" class="control-label col-md-2" />
<div class="col-md-10">
<textarea rows="4" for="Blurb"></textarea>
<span validation-for="Blurb" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
@* this <input/> lacks a "for" attribute and will not be changed by the <input/> tag helper *@
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
</form>
<div>
<a action="Index">Back to list</a>
</div>

View File

@ -0,0 +1,61 @@

@using TagHelperSample.Web.Models
@model User
<h2>Edit</h2>
<form>
<div class="form-horizontal">
<div validation-summary="true"/>
<input type="hidden" for="Id" />
<div class="form-group">
<label for="Name" class="control-label col-md-2" />
<div class="col-md-10">
<input type="text" for="Name" />
<span validation-for="Name" />
</div>
</div>
<div class="form-group">
<label for="DateOfBirth" class="control-label col-md-2" />
<div class="col-md-10">
<input type="date" for="DateOfBirth" format="{0:d}" />
<span validation-for="DateOfBirth">How old are you?</span>
</div>
</div>
<div class="form-group">
<label for="YearsEmployeed" class="control-label col-md-2" />
<div class="col-md-10">
<select for="YearsEmployeed" items="(IEnumerable<SelectListItem>)ViewBag.Items" size="2" class="form-control">
<optgroup label="Newby">
<option value="0">Less than 1</option>
<option value="1">1</option>
</optgroup>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
</select>
<span validation-for="YearsEmployeed" />
</div>
</div>
<div class="form-group">
<label for="Blurb" class="control-label col-md-2" />
<div class="col-md-10">
<textarea rows="4" for="Blurb"></textarea>
<span validation-for="Blurb" />
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Save" class="btn btn-default" />
</div>
</div>
</div>
</form>
<div>
<a action="Index">Back to list</a>
</div>

View File

@ -0,0 +1,35 @@

@using TagHelperSample.Web.Models
@model IEnumerable<User>
<h2>Index</h2>
<p>
<a action="Create">Create New</a>
</p>
@if (Model != null && Model.Count() != 0)
{
<div class="form-horizontal">
@foreach (var item in Model)
{
<div class="form-group">
<label for="@item.Name" />
<input type="text" for="@item.Name" disabled="disabled" readonly="readonly" />
</div>
<div class="form-group">
<label for="@item.DateOfBirth" />
<input type="date" for="@item.DateOfBirth" disabled="disabled" readonly="readonly" />
</div>
<div class="form-group">
<label for="@item.YearsEmployeed" />
<input type="number" for="@item.YearsEmployeed" disabled="disabled" readonly="readonly" />
</div>
<div class="form-group">
<label for="@item.Blurb" />
<textarea rows="4" for="@item.Blurb" disabled="disabled" readonly="readonly" />
</div>
<a action="Edit" controller="Home" route="MyRouteName" route-id="@item.Id">Edit</a>
}
</div>
}

View File

@ -0,0 +1,24 @@
{
"compilationOptions": {
"warningsAsErrors": true
},
"dependencies": {
"Microsoft.AspNet.Mvc": "6.0.0-*",
"Microsoft.AspNet.Server.IIS": "1.0.0-*",
"Microsoft.AspNet.Server.WebListener": "1.0.0-*",
"Microsoft.Framework.ConfigurationModel": "1.0.0-*"
},
"commands": {
"web": "Microsoft.AspNet.Hosting server=Microsoft.AspNet.Server.WebListener server.urls=http://localhost:5001"
},
"frameworks": {
"aspnet50": {
"dependencies": {
"Microsoft.Framework.ConfigurationModel.Json": "1.0.0-*"
}
},
"aspnetcore50": {
"dependencies": { }
}
}
}