コンテンツタイプをJSONに設定してAJAX経由で投稿している場合、MVC 3はコントローラアクションで正しくバインドできます。あなたの例のように、あなたはJSONを含み、通常のフォームのポストをしたい場合はMVC3は自動的にモデルにバインドしませんよう
$.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"); }
});
はしかし、あなたはコンテンツとして、いくつかのより多くの作業を行う必要がありますtypeはapplication/x-www-form-urlencodedになります。 http://blog.stevensanderson.com/2010/07/12/editing-a-variable-length-list-knockout-style/
それの要旨は、彼がどのように見える「FromJson」という属性を作成することです::
スティーブサンダーソンは、ここではあなたのコントローラのアクションに適切に拘束されることにJSONデータを提出したばかり実証古いサンプルを持っています
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を送信することを常に意味します。
すごくお礼ありがとうございます!フォームポストの結果は、ユーザーがポップアップを取得して直接開くことができるようにするためのワードドキュメントであるため、私はどのようにajaxを使用できるのか分かりません。これは正常なHTMLフォームの投稿としてすべてのブラウザで正常に動作します。しかし、あなたの他のアイデアは良いと思う。 – pilavdzice
ありがとうライアン。素晴らしい解決策。もう一度あなたは私を束縛から救い出されました:-) –