【发布时间】:2014-11-19 22:42:28
【问题描述】:
我的网页有一个 AJAX 调用,将以下数据发送到我的 MVC3 操作方法。
{
"name":"Test",
"newitems":[
{"id":15,"amount":100,"unit":"gram"},
{"id":1,"amount":75,"unit":"gram"},
{"id":46,"amount":25,"unit":"gram"}
]
}
在我的控制器中,我有以下类:
public class NewDataItem
{
public string name { get; set; }
public List<NewDataItemDetails> newitems { get; set; }
}
public class NewDataItemDetails
{
public int id { get; set; }
public int amount { get; set; }
public string unit { get; set; }
}
并且接收到请求的Action方法有一个NewDataItem作为参数。这很完美,但是 NewDataItemDetails 的 amount 属性可能并不总是包含一个 int。例如,它可能是 50.45。因此,我将public int amount { get; set; } 更改为public decimal amount { get; set; }。
在这个变化量之后总是显示为 0,而不是它在 int 时所做的正确值。
当它是一个小数时,为什么 MVC 无法将值绑定到属性,而它作为 int 工作得很好?
【问题讨论】:
-
this one 的重复?
-
对于所有输入值是否为 0,例如“5”或“5.00”?
-
@Sumo - 关闭,但我没有完全相同的问题。 ryudice 可以使用例如 5.00,但我在 5.00 和 5.00 中遇到了同样的问题。
-
在发送到服务器之前你是在做 JSON.stringify 吗?
-
@alexanderb - 是的,正如我所写的,除了我在该字段中使用十进制时工作正常,否则 ModelBinder 可以很好地读取 json。
标签: c# .net asp.net-mvc-3