【发布时间】:2014-05-28 22:46:25
【问题描述】:
我正在尝试通过 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!");
});
};
【问题讨论】:
-
奇数。我可以通过复制您的代码并使用提琴手来完成这项工作,而不必使用 Content-Type 标头,但要确保 JSON 被 [] 即列表包围。
标签: asp.net asp.net-mvc json asp.net-mvc-5