Fix for #1837 - Determine model type at runtime for TryUpdateModel and added a test
This commit is contained in:
parent
cbed666cba
commit
f1c62ef302
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in New Issue