Fix for #1837 - Determine model type at runtime for TryUpdateModel and added a test

This commit is contained in:
Kirthi Krishnamraju 2015-01-27 10:41:52 -08:00
parent cbed666cba
commit f1c62ef302
3 changed files with 33 additions and 1 deletions

View File

@ -138,7 +138,8 @@ namespace Microsoft.AspNet.Mvc
{
var modelMetadata = metadataProvider.GetMetadataForType(
modelAccessor: null,
modelType: typeof(TModel));
modelType: model.GetType());
var operationBindingContext = new OperationBindingContext
{
ModelBinder = modelBinder,

View File

@ -1429,5 +1429,26 @@ namespace Microsoft.AspNet.Mvc.FunctionalTests
Assert.Equal("test.txt", fileDetails.Filename);
Assert.Equal("Test Content", fileDetails.Content);
}
[Fact]
public async Task TryUpdateModel_ReturnDerivedAndBaseProperties()
{
// Arrange
var server = TestServer.Create(_services, _app);
var client = server.CreateClient();
// Act
var response = await client.GetStringAsync("http://localhost/TryUpdateModel/" +
"GetEmployeeAsync_BindToBaseDeclaredType" +
"?Parent.Name=fatherName&Parent.Parent.Name=grandFatherName&Department=Sales");
// Assert
var employee = JsonConvert.DeserializeObject<Employee>(response);
Assert.Equal("fatherName", employee.Parent.Name);
Assert.Equal("Sales", employee.Department);
// Round-tripped value includes descendent instances for all properties with data in the request.
Assert.Equal("grandFatherName", employee.Parent.Parent.Name);
}
}
}

View File

@ -98,6 +98,16 @@ namespace ModelBindingWebSite.Controllers
return user;
}
public async Task<Employee> GetEmployeeAsync_BindToBaseDeclaredType()
{
var employee = new Employee();
await TryUpdateModelAsync<Person>(
employee,
prefix: string.Empty);
return employee;
}
private User GetUser(int id)
{
return new User