如果您通过 AJAX 发布它并将内容类型设置为 JSON,那么 MVC 3 将能够在您的控制器操作中正确绑定它。
$.ajax({
url: location.href,
type: "POST",
data: ko.toJSON(viewModel),
datatype: "json",
contentType: "application/json charset=utf-8",
success: function (data) { alert("success"); },
error: function (data) { alert("error"); }
});
但是,如果像在您的示例中那样,您想要做一个包含 JSON 的普通表单帖子,那么您需要做更多的工作,因为 MVC3 不会自动将它绑定到您的模型,因为内容类型将是 application/x- www-form-urlencoded。
Steve Sanderson 有一个较旧的示例,该示例演示了在您的控制器操作中正确绑定提交的 JSON 数据:http: //blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-风格/
它的要点是他创建了一个名为“FromJson”的属性,如下所示:
public class FromJsonAttribute : CustomModelBinderAttribute
{
private readonly static JavaScriptSerializer serializer = new JavaScriptSerializer();
public override IModelBinder GetBinder()
{
return new JsonModelBinder();
}
private class JsonModelBinder : IModelBinder
{
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var stringified = controllerContext.HttpContext.Request[bindingContext.ModelName];
if (string.IsNullOrEmpty(stringified))
return null;
return serializer.Deserialize(stringified, bindingContext.ModelType);
}
}
}
然后,动作看起来像:
[HttpPost]
public ActionResult Index([FromJson] IEnumerable<GiftModel> gifts)
此外,如果您不喜欢使用该属性,那么您实际上可以注册一个类型以始终使用某个模型绑定器。
您可以创建一个看起来像这样的模型绑定器:
public class JsonModelBinder: IModelBinder
{
private readonly static JavaScriptSerializer serializer = new JavaScriptSerializer();
public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var stringified = controllerContext.HttpContext.Request[bindingContext.ModelName];
if (string.IsNullOrEmpty(stringified))
return null;
return serializer.Deserialize(stringified, bindingContext.ModelType);
}
}
然后,在 global.asax.cs 中注册它,例如:
ModelBinders.Binders.Add(typeof(DocumentModel), new JsonModelBinder());
现在,您不需要使用属性并且您的 DocumentModel 将被正确绑定。这意味着您将始终通过 JSON 发送 DocumentModel。