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

Commit

Permalink
Fix: Metadata ignored for non model-specific EditorTemplate
Browse files Browse the repository at this point in the history
Issue - #2778
  • Loading branch information
ajaybhargavb committed Jul 16, 2015
1 parent 6210de9 commit 92f3e21
Show file tree
Hide file tree
Showing 12 changed files with 89 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public string Build()
// We need to copy the ModelExplorer to copy the model metadata. Otherwise we might
// lose track of the model type/property. Passing null here explicitly, because
// this might be a typed VDD, and the model value might not be compatible.
var viewData = new ViewDataDictionary(_viewData, model: null);
// Create VDD of type object so it retains the correct metadata when the model type is not known.
var viewData = new ViewDataDictionary<object>(_viewData, model: null);

// We're setting ModelExplorer in order to preserve the model metadata of the original
// _viewData even though _model may be null.
Expand Down
36 changes: 36 additions & 0 deletions test/Microsoft.AspNet.Mvc.FunctionalTests/HtmlGenerationTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -539,5 +539,41 @@ public async Task FormTagHelper_GeneratesExpectedContent(bool? optionsAntiforger
Assert.Equal(expectedContent.Trim(), responseContent, ignoreLineEndingDifferences: true);
#endif
}

[Fact]
public async Task EditorTemplateWithNoModel_RendersWithCorrectMetadata()
{
// Arrange
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
var client = server.CreateClient();
var expected = PlatformNormalizer.NormalizeContent(
"<label class=\"control-label col-md-2\" for=\"Name\">ItemName</label>" + Environment.NewLine +
"<input id=\"Name\" name=\"Name\" type=\"text\" value=\"\" />" + Environment.NewLine + Environment.NewLine +
"<label class=\"control-label col-md-2\" for=\"Id\">ItemNo</label>" + Environment.NewLine +
"<input data-val=\"true\" data-val-required=\"The ItemNo field is required.\" id=\"Id\" name=\"Id\" type=\"text\" value=\"\" />" +
Environment.NewLine + Environment.NewLine);

// Act
var response = await client.GetStringAsync("http://localhost/HtmlGeneration_Home/ItemUsingSharedEditorTemplate");

// Assert
Assert.Equal(expected, response);
}

[Fact]
public async Task EditorTemplateWithSpecificModel_RendersWithCorrectMetadata()
{
// Arrange
var server = TestHelper.CreateServer(_app, SiteName, _configureServices);
var client = server.CreateClient();
var expected = "<label for=\"Description\">ItemDesc</label>" + Environment.NewLine +
"<input id=\"Description\" name=\"Description\" type=\"text\" value=\"\" />" + Environment.NewLine + Environment.NewLine;

// Act
var response = await client.GetStringAsync("http://localhost/HtmlGeneration_Home/ItemUsingModelSpecificEditorTemplate");

// Assert
Assert.Equal(expected, response);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void Activate_InstantiatesNewViewDataDictionaryType_IfTheTypeDoesNotMatch
.Returns(serviceProvider.Object);

var actionContext = new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider())
var viewData = new ViewDataDictionary<object>(new EmptyModelMetadataProvider())
{
Model = new MyModel()
};
Expand Down Expand Up @@ -190,7 +190,7 @@ public void Activate_DeterminesModelTypeFromProperty()
.Returns(serviceProvider.Object);

var actionContext = new ActionContext(httpContext.Object, new RouteData(), new ActionDescriptor());
var viewData = new ViewDataDictionary(new EmptyModelMetadataProvider());
var viewData = new ViewDataDictionary<object>(new EmptyModelMetadataProvider());
var viewContext = new ViewContext(actionContext,
Mock.Of<IView>(),
viewData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void ObjectTemplateDisplaysSimplePropertiesOnObjectByDefault()
"<div class=\"HtmlEncode[[display-label]]\">HtmlEncode[[Property1]]</div>" + Environment.NewLine
+ "<div class=\"HtmlEncode[[display-field]]\">Model = p1, ModelType = System.String, PropertyName = Property1," +
" SimpleDisplayText = p1</div>" + Environment.NewLine
+ "<div class=\"HtmlEncode[[display-label]]\">HtmlEncode[[Property2]]</div>" + Environment.NewLine
+ "<div class=\"HtmlEncode[[display-label]]\">HtmlEncode[[Prop2]]</div>" + Environment.NewLine
+ "<div class=\"HtmlEncode[[display-field]]\">Model = (null), ModelType = System.String, PropertyName = Property2," +
" SimpleDisplayText = (null)</div>" + Environment.NewLine;

Expand Down Expand Up @@ -144,7 +144,7 @@ public void ObjectTemplate_HonoursHideSurroundingHtml()
// Arrange
var expected =
"Model = p1, ModelType = System.String, PropertyName = Property1, SimpleDisplayText = p1" +
"<div class=\"HtmlEncode[[display-label]]\">HtmlEncode[[Property2]]</div>" + Environment.NewLine +
"<div class=\"HtmlEncode[[display-label]]\">HtmlEncode[[Prop2]]</div>" + Environment.NewLine +
"<div class=\"HtmlEncode[[display-field]]\">Model = (null), ModelType = System.String, PropertyName = Property2," +
" SimpleDisplayText = (null)</div>" + Environment.NewLine;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public void ObjectTemplateEditsSimplePropertiesOnObjectByDefault()
" SimpleDisplayText = p1 " +
"<span class=\"HtmlEncode[[field-validation-valid]]\" data-valmsg-for=\"HtmlEncode[[Property1]]\" data-valmsg-replace=\"HtmlEncode[[true]]\">" +
"</span></div>" + Environment.NewLine
+ "<div class=\"HtmlEncode[[editor-label]]\"><label for=\"HtmlEncode[[Property2]]\">HtmlEncode[[Property2]]</label></div>" + Environment.NewLine
+ "<div class=\"HtmlEncode[[editor-label]]\"><label for=\"HtmlEncode[[Property2]]\">HtmlEncode[[Prop2]]</label></div>" + Environment.NewLine
+ "<div class=\"HtmlEncode[[editor-field]]\">Model = (null), ModelType = System.String, PropertyName = Property2," +
" SimpleDisplayText = (null) " +
"<span class=\"HtmlEncode[[field-validation-valid]]\" data-valmsg-for=\"HtmlEncode[[Property2]]\" data-valmsg-replace=\"HtmlEncode[[true]]\">" +
Expand Down Expand Up @@ -205,7 +205,7 @@ public void ObjectTemplate_HonoursHideSurroundingHtml()
// Arrange
var expected =
"Model = p1, ModelType = System.String, PropertyName = Property1, SimpleDisplayText = p1" +
"<div class=\"HtmlEncode[[editor-label]]\"><label for=\"HtmlEncode[[Property2]]\">HtmlEncode[[Property2]]</label></div>" +
"<div class=\"HtmlEncode[[editor-label]]\"><label for=\"HtmlEncode[[Property2]]\">HtmlEncode[[Prop2]]</label></div>" +
Environment.NewLine +
"<div class=\"HtmlEncode[[editor-field]]\">" +
"Model = (null), ModelType = System.String, PropertyName = Property2, SimpleDisplayText = (null) " +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public ObjectTemplateModel()
}

public string Property1 { get; set; }
[Display(Name = "Prop2")]
public string Property2 { get; set; }
public object ComplexInnerModel { get; set; }
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,15 @@ public IActionResult Input()
{
return View();
}

public IActionResult ItemUsingSharedEditorTemplate()
{
return View();
}

public IActionResult ItemUsingModelSpecificEditorTemplate()
{
return View();
}
}
}
21 changes: 21 additions & 0 deletions test/WebSites/HtmlGenerationWebSite/Models/Item.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

using System.ComponentModel.DataAnnotations;

namespace HtmlGenerationWebSite.Models
{
public class Item
{
[UIHint("Common")]
[Display(Name = "ItemName")]
public string Name { get; set; }

[UIHint("Common")]
[Display(Name = "ItemNo")]
public int Id { get; set; }

[Display(Name = "ItemDesc")]
public string Description { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@using HtmlGenerationWebSite.Models
@model Item
@Html.Editor(nameof(Model.Description))
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
@using HtmlGenerationWebSite.Models
@model Item

@Html.EditorFor(model => model.Name)
@Html.Editor("Id")
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@Html.LabelFor(m => m, new { @class = "control-label col-md-2" })
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue)
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
@model string
@Html.LabelFor(m => m)
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue)

0 comments on commit 92f3e21

Please sign in to comment.