Fix: Metadata ignored for non model-specific EditorTemplate

Issue - #2778
This commit is contained in:
Ajay Bhargav Baaskaran 2015-07-15 11:44:26 -07:00
parent 6210de95e3
commit 92f3e21fe6
12 changed files with 89 additions and 7 deletions

View File

@ -72,7 +72,8 @@ namespace Microsoft.AspNet.Mvc.Rendering.Internal
// 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.

View File

@ -539,5 +539,41 @@ Products: Laptops (3)";
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);
}
}
}

View File

@ -112,7 +112,7 @@ namespace Microsoft.AspNet.Mvc.Razor
.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()
};
@ -190,7 +190,7 @@ namespace Microsoft.AspNet.Mvc.Razor
.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,

View File

@ -51,7 +51,7 @@ namespace Microsoft.AspNet.Mvc.Core
"<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;
@ -144,7 +144,7 @@ namespace Microsoft.AspNet.Mvc.Core
// 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;

View File

@ -97,7 +97,7 @@ namespace Microsoft.AspNet.Mvc.Core
" 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]]\">" +
@ -205,7 +205,7 @@ Environment.NewLine;
// 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) " +

View File

@ -30,6 +30,7 @@ namespace Microsoft.AspNet.Mvc.Rendering
}
public string Property1 { get; set; }
[Display(Name = "Prop2")]
public string Property2 { get; set; }
public object ComplexInnerModel { get; set; }
}

View File

@ -185,5 +185,15 @@ namespace HtmlGenerationWebSite.Controllers
{
return View();
}
public IActionResult ItemUsingSharedEditorTemplate()
{
return View();
}
public IActionResult ItemUsingModelSpecificEditorTemplate()
{
return View();
}
}
}

View File

@ -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; }
}
}

View File

@ -0,0 +1,3 @@
@using HtmlGenerationWebSite.Models
@model Item
@Html.Editor(nameof(Model.Description))

View File

@ -0,0 +1,5 @@
@using HtmlGenerationWebSite.Models
@model Item
@Html.EditorFor(model => model.Name)
@Html.Editor("Id")

View File

@ -0,0 +1,2 @@
@Html.LabelFor(m => m, new { @class = "control-label col-md-2" })
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue)

View File

@ -0,0 +1,3 @@
@model string
@Html.LabelFor(m => m)
@Html.TextBox("", ViewData.TemplateInfo.FormattedModelValue)