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

Commit 753c3ca

Browse files
committed
Address tag helper sample code review comments and reflect offline discussion
- lots of new comments in Create.cshtml
1 parent 0295b2e commit 753c3ca

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

samples/TagHelperSample.Web/Views/Home/Create.cshtml

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,53 @@
44

55
<h2>Create</h2>
66

7-
<form>
8-
@Html.AntiForgeryToken()
9-
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">
1010
<div class="form-horizontal">
11-
<validation-summary tag="div"/>
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>
1222

13-
@* element will have correct name and id attributes for Id property. but will submit a constant value. *@
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 *@
1425
<input type="hidden" for="Id" value="0" />
1526

1627
<div class="form-group">
17-
<label for="Name" class="control-label col-md-2" />
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" />
1830
<div class="col-md-10">
19-
<input type="text" for="Name" />
20-
<validation-message for="Name" tag="div" />
31+
<input type="text" for="Name" style="color:blue" />
32+
<span validation-for="Name" style="color:blue" />
2133
</div>
2234
</div>
2335
<div class="form-group">
2436
<label for="DateOfBirth" class="control-label col-md-2" />
2537
<div class="col-md-10">
26-
<input type="date" for="DateOfBirth" format="{0:d}" />
27-
<validation-message for="DateOfBirth">How old are you?</validation-message>
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>
2841
</div>
2942
</div>
3043
<div class="form-group">
3144
<label for="YearsEmployeed" class="control-label col-md-2" />
3245
<div class="col-md-10">
46+
@* <select/> tag helper has ContentBehavior.Append -- items render after static options *@
3347
<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>
3454
<optgroup label="Newby">
3555
<option value="0">Less than 1</option>
3656
<option value="1">1</option>
@@ -41,19 +61,23 @@
4161
<option value="5">5</option>
4262
<option value="6">6</option>
4363
</select>
44-
<validation-message for="YearsEmployeed" tag="div" />
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" />
4568
</div>
4669
</div>
4770
<div class="form-group">
4871
<label for="Blurb" class="control-label col-md-2" />
4972
<div class="col-md-10">
5073
<textarea rows="4" for="Blurb"></textarea>
51-
<validation-message for="Blurb" tag="div" />
74+
<span validation-for="Blurb" />
5275
</div>
5376
</div>
5477

5578
<div class="form-group">
5679
<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 *@
5781
<input type="submit" value="Create" class="btn btn-default" />
5882
</div>
5983
</div>

samples/TagHelperSample.Web/Views/Home/Edit.cshtml

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,22 @@
55
<h2>Edit</h2>
66

77
<form>
8-
@Html.AntiForgeryToken()
9-
108
<div class="form-horizontal">
11-
<validation-summary tag="div"/>
9+
<div validation-summary="true"/>
1210
<input type="hidden" for="Id" />
1311

1412
<div class="form-group">
1513
<label for="Name" class="control-label col-md-2" />
1614
<div class="col-md-10">
1715
<input type="text" for="Name" />
18-
<validation-message for="Name" tag="div" />
16+
<span validation-for="Name" />
1917
</div>
2018
</div>
2119
<div class="form-group">
2220
<label for="DateOfBirth" class="control-label col-md-2" />
2321
<div class="col-md-10">
2422
<input type="date" for="DateOfBirth" format="{0:d}" />
25-
<validation-message for="DateOfBirth">How old are you?</validation-message>
23+
<span validation-for="DateOfBirth">How old are you?</span>
2624
</div>
2725
</div>
2826
<div class="form-group">
@@ -39,14 +37,14 @@
3937
<option value="5">5</option>
4038
<option value="6">6</option>
4139
</select>
42-
<validation-message for="YearsEmployeed" tag="div" />
40+
<span validation-for="YearsEmployeed" />
4341
</div>
4442
</div>
4543
<div class="form-group">
4644
<label for="Blurb" class="control-label col-md-2" />
4745
<div class="col-md-10">
4846
<textarea rows="4" for="Blurb"></textarea>
49-
<validation-message for="Blurb" tag="div" />
47+
<span validation-for="Blurb" />
5048
</div>
5149
</div>
5250

samples/TagHelperSample.Web/Views/Home/Index.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<textarea rows="4" for="@item.Blurb" disabled="disabled" readonly="readonly" />
3030
</div>
3131

32-
<a action="Edit" controller="Home" routevalues="new { id=item.Id }">Edit</a>
32+
<a action="Edit" controller="Home" route="MyRouteName" route-id="@item.Id">Edit</a>
3333
}
3434
</div>
3535
}

0 commit comments

Comments
 (0)