3

我有以下表格:

<form id="MakeDocumentForm" name="MakeDocumentForm" 
      action="Document/GetWordDocument" method="post" 
      enctype="application/json">

    <button type="submit" style="float:right;">Make Word Document</button> 
    <textarea id="hiddenJson" 
              name="hiddenJson" 
              data-bind="text: ko.toJSON(viewModel.selectedDocument)" 
              rows="5" cols="100" 
              style="visibility:hidden;" >
    </textarea>

</form>

data-bind属性是 knockoutjs - 但这并不重要,textarea 正确包含作为序列化对象的 JSON。

[HttpPost]
public void GetWordDocument(DocumentModel hiddenJson)
{
   //hiddenJson is not a correctly populated instance of my DocumentModel class
   //any MVC experts know what I am doing wrong?
}

现在,我如何对 MVC 3 应用程序进行表单 POST 并获取反序列化的类?

4

1 回答 1

6

如果您通过 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。

于 2011-07-23T01:45:51.197 回答