Skip to content

Commit 246bb2e

Browse files
committed
Add DropDownList() and DropDownListFor() HTML helpers
- copy over legacy MVC's `SelectExtensions`, `SelectListItem` and `SelectListGroup` - plus expected `SelectList` and `MultiSelectList` - fixup select HTML helpers to meet WebFx standards and work in new world - usual stuff: `[NotNull]`, `var`, `String` -> `string`, long lines, ... - remove `IDictionary<string, object> htmlAttributes` overloads - move longest extension method overloads into correct classes / interfaces - add `ViewDataEvaluator.Eval()` overload for an `object` container - rename lower-level helpers to make purposes more obvious - nit: move Raw() methods up from bottom of HtmlHelper.cs - use `DropDownList[For]()` in MVC sample
1 parent 271c849 commit 246bb2e

File tree

16 files changed

+778
-14
lines changed

16 files changed

+778
-14
lines changed

samples/MvcSample.Web/Areas/Travel/Views/Flight/Fly.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
<div class="col-md-4">
1515
<h2>Back to the main Area.</h2>
1616
<p>Takes you out of the area implicitly.</p>
17-
<p><a class="btn btn-default" href="@Url.Action("Edit", "Home")">Go to Home/Edit</a></p>
17+
<p><a class="btn btn-default" href="@Url.Action("Create", "Home")">Go to Home/Create</a></p>
1818
</div>
1919
<div class="col-md-4">
2020
<h2>Go to another action in the Area.</h2>

samples/MvcSample.Web/HomeController.cs

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
1-
using Microsoft.AspNet.Mvc;
1+
using System.Collections.Generic;
2+
using System.Linq;
3+
using Microsoft.AspNet.Mvc;
4+
using Microsoft.AspNet.Mvc.Rendering;
25
using MvcSample.Web.Models;
36

47
namespace MvcSample.Web
58
{
69
public class HomeController : Controller
710
{
11+
private static readonly IEnumerable<SelectListItem> _addresses = CreateAddresses();
12+
private static readonly IEnumerable<SelectListItem> _ages = CreateAges();
13+
814
public IActionResult Index()
915
{
1016
return View("MyView", User());
@@ -22,17 +28,21 @@ public IActionResult ValidationSummary()
2228
/// </summary>
2329
public IActionResult Create()
2430
{
31+
ViewBag.Address = _addresses;
32+
ViewBag.Ages = _ages;
33+
2534
return View();
2635
}
2736

2837
/// <summary>
2938
/// Action that shows metadata when model is non-<c>null</c>.
3039
/// </summary>
31-
/// <returns></returns>
32-
public IActionResult Edit()
40+
public IActionResult Edit(User user)
3341
{
42+
ViewBag.Address = _addresses;
43+
ViewBag.Age = _ages;
3444
ViewBag.Gift = "the banana";
35-
ViewData.Model = new User { Name = "Name", Address = "Address in a State", Age = 37, };
45+
3646
return View("Create");
3747
}
3848

@@ -98,5 +108,27 @@ public IActionResult MyView()
98108
{
99109
return View(User());
100110
}
111+
112+
private static IEnumerable<SelectListItem> CreateAddresses()
113+
{
114+
var addresses = new[]
115+
{
116+
"121 Fake St., Redmond, WA, USA",
117+
"123 Fake St., Redmond, WA, USA",
118+
"125 Fake St., Redmond, WA, USA",
119+
"127 Fake St., Redmond, WA, USA",
120+
"129 Fake St., Redmond, WA, USA",
121+
"131 Fake St., Redmond, WA, USA",
122+
};
123+
124+
return new SelectList(addresses);
125+
}
126+
127+
private static IEnumerable<SelectListItem> CreateAges()
128+
{
129+
var ages = Enumerable.Range(27, 47).Select(age => new { Age = age, Display = age.ToString("####"), });
130+
131+
return new SelectList(ages, dataValueField: "Age", dataTextField: "Display");
132+
}
101133
}
102134
}

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

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,46 @@
3838
}
3939
</div>
4040

41+
<div class="row">
42+
<div style="float: left; border: thick solid limegreen; padding-right: 10px">
43+
@using (Html.BeginForm(controllerName: "Home", actionName: "Edit", method: FormMethod.Post))
44+
{
45+
<table>
46+
<tr>
47+
<td>
48+
<label class="control-label col-md-2">Model.Name</label>
49+
</td>
50+
<td>
51+
@Html.TextBox("Name")
52+
</td>
53+
</tr>
54+
<tr>
55+
<td>
56+
<label class="control-label col-md-2">Model.Address</label>
57+
</td>
58+
<td>
59+
@Html.DropDownList("Address", "Select an Address")
60+
</td>
61+
</tr>
62+
<tr>
63+
<td>
64+
<label class="control-label col-md-2">Model.Age</label>
65+
</td>
66+
<td>
67+
@Html.DropDownListFor(model => model.Age, (IEnumerable<SelectListItem>)ViewBag.Ages, htmlAttributes: new { @class = "form-control" })
68+
</td>
69+
</tr>
70+
<tr>
71+
<td>
72+
<input type="submit" value="Save" class="btn btn-default" style="margin-left: 10px" />
73+
</td>
74+
<td></td>
75+
</tr>
76+
</table>
77+
}
78+
</div>
79+
</div>
80+
4181
@helper PropertyListItem(ModelMetadata property)
4282
{
4383
var propertyName = property.PropertyName;

samples/MvcSample.Web/Views/Shared/HelloWorldPartial.cshtml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33

44
<strong>Hello @Model.Name from Partial</strong>
55

6-
<a href="@Url.Action("Edit", "Home")">Edit Something!</a>
6+
<a href="@Url.Action("Create", "Home")">Create Something!</a>

src/Microsoft.AspNet.Mvc.Core/Microsoft.AspNet.Mvc.Core.kproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<Compile Include="Rendering\HtmlHelperLinkExtensions.cs" />
142142
<Compile Include="Rendering\HtmlHelperNameExtensions.cs" />
143143
<Compile Include="Rendering\HtmlHelperPartialExtensions.cs" />
144+
<Compile Include="Rendering\HtmlHelperSelectExtensions.cs" />
144145
<Compile Include="Rendering\HtmlHelperValidationExtensions.cs" />
145146
<Compile Include="Rendering\HtmlHelperValueExtensions.cs" />
146147
<Compile Include="Rendering\HtmlString.cs" />
@@ -156,7 +157,11 @@
156157
<Compile Include="Rendering\IHtmlHelperOfT.cs" />
157158
<Compile Include="Rendering\IView.cs" />
158159
<Compile Include="Rendering\IViewEngine.cs" />
160+
<Compile Include="Rendering\MultiSelectList.cs" />
159161
<Compile Include="Rendering\MvcForm.cs" />
162+
<Compile Include="Rendering\SelectList.cs" />
163+
<Compile Include="Rendering\SelectListGroup.cs" />
164+
<Compile Include="Rendering\SelectListItem.cs" />
160165
<Compile Include="Rendering\ViewEngineResult.cs" />
161166
<Compile Include="RouteConstraintAttribute.cs" />
162167
<Compile Include="RouteDataActionConstraint.cs" />

src/Microsoft.AspNet.Mvc.Core/Properties/Resources.Designer.cs

Lines changed: 48 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Microsoft.AspNet.Mvc.Core/Rendering/Expressions/ViewDataEvaluator.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,12 @@ public static ViewDataInfo Eval([NotNull] ViewDataDictionary viewData, [NotNull]
2020
return EvalComplexExpression(viewData, expression);
2121
}
2222

23+
public static ViewDataInfo Eval(object indexableObject, [NotNull] string expression)
24+
{
25+
// Run through same cases as other Eval() overload but allow a null container.
26+
return (indexableObject == null) ? null : EvalComplexExpression(indexableObject, expression);
27+
}
28+
2329
private static ViewDataInfo EvalComplexExpression(object indexableObject, string expression)
2430
{
2531
foreach (var expressionPair in GetRightToLeftExpressions(expression))

0 commit comments

Comments
 (0)