将表单数据传递给控制器​​方法 - 子属性未填充

发布于 2024-11-06 21:16:14 字数 1299 浏览 0 评论 0原文

我的应用程序在客户端使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷遇一生 2024-11-13 21:16:14

实际上,如果您希望默认模型绑定器成功绑定这些值,则请求应该如下所示(注意 PhoneNumbers[0].CustomerID 而不是 PhoneNumbers[0][CustomerID] ):

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

您可以查看以下博客文章,了解用于集合的有线格式。

作为替代方案,您可以使用 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 of PhoneNumbers[0][CustomerID]):

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

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文