我正在尝试通过JSON将一组对象发布到我的控制器。发送单个对象时,MVC模型绑定工作正常,但是当我尝试发送一个对象数组时,绑定完全失败,我在控制器操作中收到一个空对象。
首先,这是我正在使用的ViewModel:
public class ForecastViewModel
{
public int RestaurantCode { get; set; }
public System.DateTime FiscalDate { get; set; }
public Nullable<decimal> ForecastSales { get; set; }
public Nullable<decimal> ForecastFOHLabour { get; set; }
public Nullable<decimal> ForecastFOHHours { get; set; }
public Nullable<decimal> ForecastBOHLabour { get; set; }
public Nullable<decimal> ForecastBOHHours { get; set; }
public Nullable<decimal> ForecastFoodSales { get; set; }
public ForecastViewModel() { }
public ForecastViewModel(Forecast model){...}
}
控制器动作:
[HttpPost]
public ActionResult Update(List<ForecastViewModel> model){...}
这是发送给我的控制器的原始JSON:
[{"RestaurantCode":1002,"FiscalDate":"2/24/2014","ForecastSales":"111","ForecastFOHLabour":"111.11","ForecastFOHHours":"11","ForecastBOHLabour":"111.11","ForecastBOHHours":"11","ForecastFoodSales":"111.11","FOHPercent":"100.1","BOHPercent":"100.0"},{"RestaurantCode":1002,"FiscalDate":"2/25/2014","ForecastSales":"111","ForecastFOHLabour":"111.11","ForecastFOHHours":"11","ForecastBOHLabour":"111.11","ForecastBOHHours":"11","ForecastFoodSales":"111.11","FOHPercent":"100.1","BOHPercent":"100.0"},{"RestaurantCode":1002,"FiscalDate":"2/26/2014","ForecastSales":"111","ForecastFOHLabour":"111.11","ForecastFOHHours":"11","ForecastBOHLabour":"111.11","ForecastBOHHours":"11","ForecastFoodSales":"111.11","FOHPercent":"100.1","BOHPercent":"100.0"},{"RestaurantCode":1002,"FiscalDate":"2/27/2014","ForecastSales":"111","ForecastFOHLabour":"111.11","ForecastFOHHours":"11","ForecastBOHLabour":"111.11","ForecastBOHHours":"11","ForecastFoodSales":"111.11","FOHPercent":"100.1","BOHPercent":"100.0"},{"RestaurantCode":1002,"FiscalDate":"2/28/2014","ForecastSales":"111","ForecastFOHLabour":"111.11","ForecastFOHHours":"11","ForecastBOHLabour":"111.11","ForecastBOHHours":"11","ForecastFoodSales":"111.11","FOHPercent":"100.1","BOHPercent":"100.0"},{"RestaurantCode":1002,"FiscalDate":"2/29/2014","ForecastSales":"111","ForecastFOHLabour":"111.11","ForecastFOHHours":"11","ForecastBOHLabour":"111.11","ForecastBOHHours":"11","ForecastFoodSales":"111.11","FOHPercent":"100.1","BOHPercent":"100.0"},{"RestaurantCode":1002,"FiscalDate":"2/30/2014","ForecastSales":"111","ForecastFOHLabour":"111.11","ForecastFOHHours":"11","ForecastBOHLabour":"111.11","ForecastBOHHours":"11","ForecastFoodSales":"111.11","FOHPercent":"100.1","BOHPercent":"100.0"}]
我回去修改代码,将ForecastViewModel的单个实例发送到我的控制器而不是数组,并且绑定工作完美:
[HttpPost]
public ActionResult Update(ForecastViewModel model){...}
这是我发送控制器操作的JSON:
{"RestaurantCode":1002,"FiscalDate":"2/24/2014","ForecastSales":"111","ForecastFOHLabour":"111.11","ForecastFOHHours":"11","ForecastBOHLabour":"111.11","ForecastBOHHours":"11","ForecastFoodSales":"111.11","FOHPercent":"100.1","BOHPercent":"100.0"}
如何修改代码以使绑定对象对象数组起作用?
更新: 这是发送请求的JavaScript。我正在使用knockout.js将我的对象数组序列化为JSON。
self.save = function () {
var forecasts = ko.toJSON(self.forecasts);
console.log(forecasts)
$.ajax({
type: "POST",
url: "/Tools/Forecasts/Update/",
dataType: "application/json",
data: forecasts
}).done(function(data){
alert("complete!");
});
};
答案 0 :(得分:4)
"application/json"
不是$.ajax中dataType
参数的有效值(此参数无论如何都要配置应如何解析来自服务器的数据并且它没有任何内容处理发送到服务器的数据格式。
您需要设置的是contentType
选项:
$.ajax({
type: "POST",
url: "/Tools/Forecasts/Update/",
contentType: "application/json",
data: forecasts
}).done(function(data){
alert("complete!");
});