Skip to content

Commit

Permalink
修复ArrayModelBinder的BUG
Browse files Browse the repository at this point in the history
  • Loading branch information
Surbowl committed Feb 7, 2020
1 parent 64f44af commit 302e218
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
10 changes: 5 additions & 5 deletions Routine/Routine.APi/Controllers/CompanyCollectionsController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ public async Task<IActionResult> GetCompanyCollection([FromRoute]
{
return BadRequest();
}

var entities = await _companyRepository.GetCompaniesAsync(ids);
//这个写法有 BUG,ids.Count() 返回的居然是字符串长度
//if (ids.Count() != entities.Count())
//{
// return NotFound();
//}
if (ids.Count() != entities.Count())
{
return NotFound();
}
var dtosToReturn = _mapper.Map<IEnumerable<CompanyDto>>(entities);
return Ok(dtosToReturn);
}
Expand Down
2 changes: 1 addition & 1 deletion Routine/Routine.APi/Helpers/ArrayModelBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Task BindModelAsync(ModelBindingContext bindingContext)
var converter = TypeDescriptor.GetConverter(elementType);
var values = value.Split(new[] { "," }, StringSplitOptions.RemoveEmptyEntries)
.Select(x => converter.ConvertFromString(x.Trim())).ToArray();
var typedValues = Array.CreateInstance(elementType, value.Length);
var typedValues = Array.CreateInstance(elementType, values.Length);
values.CopyTo(typedValues, 0);
bindingContext.Model = typedValues;
bindingContext.Result = ModelBindingResult.Success(bindingContext.Model);
Expand Down

0 comments on commit 302e218

Please sign in to comment.