将表单数据传递给控制器方法 - 子属性未填充
我的应用程序在客户端使用 JavascriptMVC,而 ASP MVC 基本上仅作为 REST 服务运行。这是一个典型的控制器方法:
public JsonResult Update(CustomerDto dto)
{
var repository = Factory.NewCustomerRepository())
// ... Convert DTO back to entity and save changes
return Json(dto);
}
问题是,我的 CustomerDTO 包含一些属性,这些属性没有从表单数据转换为它们应该是的对象。例如,PhoneNumbers:
public class CustomerDto
{
public int Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public PhoneNumberDto[] PhoneNumbers { get; set; }
// ... more properties
}
public class PhoneNumberDTO
{
public int Id { get; set; }
public int CustomerId { get; set; }
public string Label { get; set; }
public string Number { get; set; }
}
在控制器操作中,PhoneNumbers 将在数组中具有正确数量的元素,但每个对象仅具有 null/默认值。我已经验证该请求正在发送所有适当的表单数据:
Id 26
FirstName A
LastName Person
MiddleName Test
PhoneNumbers[0][CustomerID 26
PhoneNumbers[0][Id] 5
PhoneNumbers[0][Label] Mobile
PhoneNumbers[0][Number] (555)555-5555
PhoneNumbers[1][CustomerID 26
PhoneNumbers[1][Id] 8
PhoneNumbers[1][Label] Home
PhoneNumbers[1][Number] (654)654-6546
对可能发生的情况有什么想法吗?我认为 MVC3 可以自动从表单值映射嵌套对象是错误的吗?感谢您的帮助!
My app is using JavascriptMVC on the client side, and ASP MVC is basically functioning only as a REST service. Here's a typical controller method:
public JsonResult Update(CustomerDto dto)
{
var repository = Factory.NewCustomerRepository())
// ... Convert DTO back to entity and save changes
return Json(dto);
}
The problem is, my CustomerDTO contains some properties that aren't getting converted from the form data into the objects that they should be. For example, PhoneNumbers:
public class CustomerDto
{
public int Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public PhoneNumberDto[] PhoneNumbers { get; set; }
// ... more properties
}
public class PhoneNumberDTO
{
public int Id { get; set; }
public int CustomerId { get; set; }
public string Label { get; set; }
public string Number { get; set; }
}
In the controller action, PhoneNumbers will have the correct number of elements in the array, but each object will have only null/default values. I've verified that the request is sending all of the appropriate form data:
Id 26
FirstName A
LastName Person
MiddleName Test
PhoneNumbers[0][CustomerID 26
PhoneNumbers[0][Id] 5
PhoneNumbers[0][Label] Mobile
PhoneNumbers[0][Number] (555)555-5555
PhoneNumbers[1][CustomerID 26
PhoneNumbers[1][Id] 8
PhoneNumbers[1][Label] Home
PhoneNumbers[1][Number] (654)654-6546
Any ideas on what could be going on? Am I just wrong in thinking that MVC3 can map nested objects from the form values automatically? Thanks for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
实际上,如果您希望默认模型绑定器成功绑定这些值,则请求应该如下所示(注意
PhoneNumbers[0].CustomerID
而不是PhoneNumbers[0][CustomerID]
):您可以查看以下博客文章,了解用于集合的有线格式。
作为替代方案,您可以使用 JSON 请求。
Actually the request should look like this if you want the default model binder to successfully bind those values (notice
PhoneNumbers[0].CustomerID
instead ofPhoneNumbers[0][CustomerID]
):You may take a look at the following blog post for the wire format used for collections.
As an alternative you could use JSON requests.