С этой моделью контакта
public class Contact
{
public string Name { get; set; }
public ICollection<Phone> Phones { get; set; }
public Phone PrimaryPhone
{
get { return Phones.FirstOrDefault(x => x.Primary) ?? new Phone(); }
}
}
public class Phone
{
public bool Primary { get; set; }
public string PhoneNumber { get; set; }
public string Type { get; set; }
}
И с этим контроллером
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public ActionResult Index(Contact contact)
{
return View();
}
}
Когда я POST к индексу HomeController, используя jQuery
(function ($) {
var myData = {
Name: 'Wesley Crusher',
Phones: [
{ Primary: false, PhoneNumber: '111-111-1111', Type: 'Business' },
{ Primary: true, PhoneNumber: '222-222-2222', Type: 'Personal' },
{ Primary: false, PhoneNumber: '333-333-3333', Type: 'Business' }
],
PrimaryPhone: { Primary: true, PhoneNumber: '111-111-1111', Type: 'Business' }
};
$.ajax({
url: '@Url.Action("Index", "Home")',
type: 'POST',
contentType: 'application/json',
data: JSON.stringify(myData)
});
})(jQuery)
связыватель модели неправильно создает телефоны ICollection. Данные:
- [0] Primary = false, PhoneNumber = "111-111-1111", Type = "Business" MVC3ModelBinderJsonTesting.Models.Phone
- [1] Primary = true, PhoneNumber = "111-111-1111", Type = "Business" MVC3ModelBinderJsonTesting.Models.Phone
- [2] Primary = false, PhoneNumber ="333-333-3333", Type = "Business" MVC3ModelBinderJsonTesting.Models.Phone
Номер телефона "111-111-1111" повторяется, и типом является "Business" вместо«Личное». Это ожидаемое поведение по какой-то причине или это ошибка?
Я могу опубликовать пример проекта, если хотите, дайте мне знать.