From bf00f478e5681ea64062bb59c205116cfe2a3ace Mon Sep 17 00:00:00 2001 From: Doug Bunting Date: Mon, 19 Jan 2015 15:14:01 -0800 Subject: [PATCH] Add another view to MvcTagHelpersWebSite using HTML helpers - with some `` hacks, generated HTML is almost identical to tag helper version - attribute order (HTML helpers consistently order alphabetically) is primary difference - bit more testing, therefore related to #453 nits: - remove some trailing whitespace - clean up style in `MvcTagHelperTest[s]` and `MvcTagHelper_HomeController` - e.g. more init syntax, fewer duplicate variables - correct "MvcTagHelperTest~~s~~" file / class name - remove unused `Order.OrderNumber` property in MvcTagHelpersWebSite project - correct one spelling mistake --- ...cTagHelper_Home.OrderUsingHtmlHelpers.html | 76 ++++++++++++ ...WebSite.MvcTagHelper_Home.ProductList.html | 2 +- ...agHelpersTests.cs => MvcTagHelpersTest.cs} | 27 ++--- .../MvcTagHelper_HomeController.cs | 108 ++++++++++-------- .../MvcTagHelpersWebSite/Models/Order.cs | 6 - .../GenderUsingHtmlHelpers.cshtml | 4 + .../OrderUsingHtmlHelpers.cshtml | 76 ++++++++++++ 7 files changed, 232 insertions(+), 67 deletions(-) create mode 100644 test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.html rename test/Microsoft.AspNet.Mvc.FunctionalTests/{MvcTagHelpersTests.cs => MvcTagHelpersTest.cs} (93%) create mode 100644 test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/EditorTemplates/GenderUsingHtmlHelpers.cshtml create mode 100644 test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/OrderUsingHtmlHelpers.cshtml diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.html new file mode 100644 index 0000000000..f617c0eb65 --- /dev/null +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/MvcTagHelpersWebSite.MvcTagHelper_Home.OrderUsingHtmlHelpers.html @@ -0,0 +1,76 @@ + + + + + + + + + +
+
+ + +
+
+ + +
+
+ + + +
+
+ + +
+
+ + +
+
+ + +
+
+ + + +
+
+ + +
+
+ + + +
+
+ + +
+
+ + + +
+
+ + Male + Female + +
+
  • +
+ + +
+ + \ No newline at end of file diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/MvcTagHelpersWebSite.MvcTagHelper_Home.ProductList.html b/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/MvcTagHelpersWebSite.MvcTagHelper_Home.ProductList.html index d71b0100a0..7d97032b7e 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/MvcTagHelpersWebSite.MvcTagHelper_Home.ProductList.html +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/Compiler/Resources/MvcTagHelpersWebSite.MvcTagHelper_Home.ProductList.html @@ -54,7 +54,7 @@
+Product_2 description
diff --git a/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTagHelpersTests.cs b/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTagHelpersTest.cs similarity index 93% rename from test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTagHelpersTests.cs rename to test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTagHelpersTest.cs index 6a3bcac8d2..f7dfc0d52b 100644 --- a/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTagHelpersTests.cs +++ b/test/Microsoft.AspNet.Mvc.FunctionalTests/MvcTagHelpersTest.cs @@ -15,23 +15,26 @@ using Xunit; namespace Microsoft.AspNet.Mvc.FunctionalTests { - public class MvcTagHelpersTests + public class MvcTagHelpersTest { private readonly IServiceProvider _provider = TestHelper.CreateServices("MvcTagHelpersWebSite"); private readonly Action _app = new Startup().Configure; - private static readonly Assembly _resourcesAssembly = typeof(MvcTagHelpersTests).GetTypeInfo().Assembly; + private static readonly Assembly _resourcesAssembly = typeof(MvcTagHelpersTest).GetTypeInfo().Assembly; [Theory] [InlineData("Index", null)] + // Test ability to generate nearly identical HTML with MVC tag and HTML helpers. + // Only attribute order should differ. [InlineData("Order", "/MvcTagHelper_Order/Submit")] + [InlineData("OrderUsingHtmlHelpers", "/MvcTagHelper_Order/Submit")] [InlineData("Product", null)] [InlineData("Customer", "/Customer/MvcTagHelper_Customer")] - // Testing InputTagHelpers invoked in the partial views - [InlineData("ProductList", null)] + // Testing InputTagHelpers invoked in the partial views + [InlineData("ProductList", null)] // Testing MvcTagHelpers invoked in the editor templates with the HTML helpers - [InlineData("EmployeeList", null)] - // Testing SelectTagHelper with Html.BeginForm - [InlineData("CreateWarehouse", null)] + [InlineData("EmployeeList", null)] + // Testing SelectTagHelper with Html.BeginForm + [InlineData("CreateWarehouse", null)] // Testing the HTML helpers with FormTagHelper [InlineData("EditWarehouse", null)] // Testing the EnvironmentTagHelper @@ -45,9 +48,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // The K runtime compiles every file under compiler/resources as a resource at runtime with the same name // as the file name, in order to update a baseline you just need to change the file in that folder. - var expectedContent = - await _resourcesAssembly.ReadResourceAsStringAsync - ("compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home." + action + ".html"); + var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync( + "compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Home." + action + ".html"); // Act // The host is not important as everything runs in memory and tests are isolated from each other. @@ -72,9 +74,8 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests // Arrange var server = TestServer.Create(_provider, _app); var client = server.CreateClient(); - var expectedContent = - await _resourcesAssembly.ReadResourceAsStringAsync - ("compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Customer.Index.html"); + var expectedContent = await _resourcesAssembly.ReadResourceAsStringAsync( + "compiler/resources/MvcTagHelpersWebSite.MvcTagHelper_Customer.Index.html"); var request = new HttpRequestMessage(HttpMethod.Post, "http://localhost/Customer/MvcTagHelper_Customer"); var nameValueCollection = new List> diff --git a/test/WebSites/MvcTagHelpersWebSite/Controllers/MvcTagHelper_HomeController.cs b/test/WebSites/MvcTagHelpersWebSite/Controllers/MvcTagHelper_HomeController.cs index a3f52e1098..83c3d697af 100644 --- a/test/WebSites/MvcTagHelpersWebSite/Controllers/MvcTagHelper_HomeController.cs +++ b/test/WebSites/MvcTagHelpersWebSite/Controllers/MvcTagHelper_HomeController.cs @@ -3,7 +3,6 @@ using System; using System.Collections.Generic; -using System.Linq; using Microsoft.AspNet.Mvc; using Microsoft.AspNet.Mvc.Rendering; using MvcTagHelpersWebSite.Models; @@ -12,48 +11,60 @@ namespace MvcTagHelpersWebSite.Controllers { public class MvcTagHelper_HomeController : Controller { - private readonly List _products = new List(); - - public MvcTagHelper_HomeController() + private readonly List _products = new List { - _products.Add(new Product + new Product { ProductName = "Product_0", Number = 0, HomePage = new Uri("http://www.contoso.com") - }); - _products.Add(new Product + }, + new Product { ProductName = "Product_1", Number = 1, - }); - _products.Add(new Product + }, + new Product { ProductName = "Product_2", Number = 2, - Description = "Product_2 desription" - }); + Description = "Product_2 description" + }, + }; + private readonly IEnumerable _productsList; + private readonly IEnumerable _productsListWithSelection; + private readonly Order _order = new Order + { + Shipping = "UPSP", + Customer = new Customer + { + Key = "KeyA", + Number = 1, + Gender = Gender.Female, + Name = "NameStringValue", + }, + NeedSpecialHandle = true, + PaymentMethod = new List { "Check" }, + }; + + public MvcTagHelper_HomeController() + { + _productsList = new SelectList(_products, "Number", "ProductName"); + _productsListWithSelection = new SelectList(_products, "Number", "ProductName", 2); } public IActionResult Order() { - ViewBag.Items = new SelectList(_products, "Number", "ProductName", 2); + ViewBag.Items = _productsListWithSelection; - var order = new Order - { - Shipping = "UPSP", - Customer = new Customer - { - Key = "KeyA", - Number = 1, - Gender = Gender.Female, - Name = "NameStringValue", - }, - NeedSpecialHandle = true, - PaymentMethod = new List { "Check" } - }; + return View(_order); + } - return View(order); + public IActionResult OrderUsingHtmlHelpers() + { + ViewBag.Items = _productsListWithSelection; + + return View(_order); } public IActionResult Product() @@ -63,6 +74,7 @@ namespace MvcTagHelpersWebSite.Controllers HomePage = new System.Uri("http://www.contoso.com"), Description = "Type the product description" }; + return View(product); } @@ -88,34 +100,35 @@ namespace MvcTagHelpersWebSite.Controllers public IActionResult EmployeeList() { - var employees = new List(); - - employees.Add(new Employee + var employees = new List { - Name = "EmployeeName_0", - Number = 0, - Address = "Employee_0 address" - }); - employees.Add(new Employee - { - Name = "EmployeeName_1", - Number = 1, - OfficeNumber = "1002", - Gender = Gender.Female - }); - employees.Add(new Employee - { - Name = "EmployeeName_2", - Number = 2, - Remote = true - }); + new Employee + { + Name = "EmployeeName_0", + Number = 0, + Address = "Employee_0 address" + }, + new Employee + { + Name = "EmployeeName_1", + Number = 1, + OfficeNumber = "1002", + Gender = Gender.Female + }, + new Employee + { + Name = "EmployeeName_2", + Number = 2, + Remote = true + }, + }; return View(employees); } public IActionResult CreateWarehouse() { - ViewBag.Items = new SelectList(_products, "Number", "ProductName", 9); + ViewBag.Items = _productsList; return View(); } @@ -134,6 +147,7 @@ namespace MvcTagHelpersWebSite.Controllers Gender = Gender.Female } }; + return View(warehouse); } diff --git a/test/WebSites/MvcTagHelpersWebSite/Models/Order.cs b/test/WebSites/MvcTagHelpersWebSite/Models/Order.cs index 8878234937..67c17dad60 100644 --- a/test/WebSites/MvcTagHelpersWebSite/Models/Order.cs +++ b/test/WebSites/MvcTagHelpersWebSite/Models/Order.cs @@ -14,12 +14,6 @@ namespace MvcTagHelpersWebSite.Models set; } - public int OrderNumber - { - get; - set; - } - public DateTimeOffset OrderDate { get; diff --git a/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/EditorTemplates/GenderUsingHtmlHelpers.cshtml b/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/EditorTemplates/GenderUsingHtmlHelpers.cshtml new file mode 100644 index 0000000000..bfb24e4fc2 --- /dev/null +++ b/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/EditorTemplates/GenderUsingHtmlHelpers.cshtml @@ -0,0 +1,4 @@ +@using MvcTagHelpersWebSite.Models +@model Gender +@Html.RadioButtonFor(m => m, value: "Male") Male +@Html.RadioButtonFor(m => m, value: "Female") Female \ No newline at end of file diff --git a/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/OrderUsingHtmlHelpers.cshtml b/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/OrderUsingHtmlHelpers.cshtml new file mode 100644 index 0000000000..a52d352f29 --- /dev/null +++ b/test/WebSites/MvcTagHelpersWebSite/Views/MvcTagHelper_Home/OrderUsingHtmlHelpers.cshtml @@ -0,0 +1,76 @@ +@model MvcTagHelpersWebSite.Models.Order + +@{ + ViewBag.Title = "Order Page"; +} + + + + + + + + + @* Use comments and elements to force whitespace in generated HTML. *@@using ( +Html.BeginForm(actionName: "Submit", controllerName: "MvcTagHelper_Order")) + { +
+ @Html.LabelFor(m => m.Shipping, htmlAttributes: new { @class = "order" }) + @Html.TextBoxFor(m => m.Shipping, format: "Your shipping method is {0}", htmlAttributes: new { size = "50" }) +
+
+ @Html.LabelFor(m => m.ShippingDateTime, htmlAttributes: new { @class = "order" }) + @Html.TextBoxFor(m => m.ShippingDateTime, htmlAttributes: new { type = "datetime-local" }) +
+
+ @Html.LabelFor(m => m.Products, htmlAttributes: new { @class = "order" }) + @{ var productSelectList = (IEnumerable)ViewBag.Items; } + @Html.ListBoxFor(m => m.Products, productSelectList) +
+
+ @Html.LabelFor(m => m.OrderDate, htmlAttributes: new { @class = "order" }) + @Html.TextBoxFor(m => m.OrderDate, format: "{0:yyyy/MM/dd/ g}", htmlAttributes: new { type = "datetime" }) +
+
+ @Html.LabelFor(m => m.NeedSpecialHandle, htmlAttributes: new { @class = "order" }) + @Html.CheckBoxFor(m => m.NeedSpecialHandle) +
+
+ @Html.LabelFor(m => m.PaymentMethod, htmlAttributes: new { @class = "order" }) + @Html.ListBoxFor(m => m.PaymentMethod, selectList: new SelectList(new[] { new { value = "Credit" }, new { value = "Check" } }, dataValueField: "value", dataTextField: "value")) +
+
+ @Html.LabelFor(m => m.Customer.Number, htmlAttributes: new { @class = "order" }) + @Html.TextBoxFor(m => m.Customer.Number, htmlAttributes: new { type = "number", @class = "form-control" }) + @Html.ValidationMessageFor(m => m.Customer.Number) +
+
+ @Html.LabelFor(m => m.Customer.Name, htmlAttributes: new { @class = "order" }) + @Html.TextBoxFor(m => m.Customer.Name) +
+
+ @Html.LabelFor(m => m.Customer.Email, htmlAttributes: new { @class = "order" }) + @Html.TextBoxFor(m => m.Customer.Email, htmlAttributes: new { type = "email" }) + @Html.ValidationMessageFor(m => m.Customer.Email) +
+
+ @Html.LabelFor(m => m.Customer.PhoneNumber, htmlAttributes: new { @class = "order" }) + @Html.TextBoxFor(m => m.Customer.PhoneNumber, htmlAttributes: new { type = "tel" }) +
+
+ @Html.LabelFor(m => m.Customer.Password, htmlAttributes: new { @class = "order" }) + @Html.PasswordFor(m => m.Customer.Password, htmlAttributes: new { @class = "form-control" }) + @Html.ValidationMessageFor(m => m.Customer.Password) +
+
+ @Html.LabelFor(m => m.Customer.Gender, htmlAttributes: new { @class = "order" }) + @Html.EditorFor(model => model.Customer.Gender, templateName: "GenderUsingHtmlHelpers") + @Html.ValidationMessageFor(m => m.Customer.Gender) +
+ @Html.ValidationSummary(message: null, htmlAttributes: new { @class = "order" }) + @Html.HiddenFor(m => m.Customer.Key) + + @Html.AntiForgeryToken(); + }@**@ + + \ No newline at end of file