JSON / MVC (3P1) HttpPost - EF クラスで動作しない
-
25-09-2019 - |
質問
古いチュートリアルと MVC 3 Preview 1 に関する最近の投稿を組み合わせて一致させると、次の問題が発生します。JSON 駆動の編集に移行しようとしています Fighter
JSON を使用しない「単純な古い」編集ではなく、モデル (および基礎となるデータベース) を編集します。
編集ビューがあります( Shared EditorTemplate
, fighter.ascx
) 私のセットアップ Fighter
クラス (EF 4 モデルに存在します)。
これにはボタンが2つあります。1 つは「古い」もので、JSON なしで editcontroller を呼び出す送信で、もう 1 つは新しいもので、新しいものを作成しました。 HttpPost ActionResult
古いボタンは機能します。新しいボタンは半分しか実装されていませんが、すでに ActionResult UpdateJsonTrick
ビューからデータを正しく受信していません。の returnMessage
文字列の読み取り:"システム内にファイター「」を作成" そのActionResultで何か役に立つことをする前に、そのデータを渡す方法を知らなければならない。私のどこが間違っているのでしょうか?
したがって、edit.aspx は単なる単純なものです。 Html.EditorForModel("Fighter")
声明、しかし、ここにFighter.ascxがあります:
<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<Mvc3_EF_BW_Fight.Models.Fighter>" %>
<% using (Html.BeginForm())
{%>
<%: Html.ValidationSummary(true) %>
<script type="text/javascript" src="../../../Scripts/jquery-1.4.1.min.js"></script>
<script type="text/javascript" src="../../../Scripts/json2.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#JSONTRICK").click(function (event) {
var fighter = { Id: $('#Id').val(),
FighterName: $('#FighterName').val(),
FighterStyleDescription: $('#FighterStyleDescription').val(),
FighterLongDescription: $('#FighterLongDescription').val()
};
$.ajax({
url: '/Barracks/UpdateJsonTrick',
type: "POST",
data: JSON.stringify(fighter),
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
// get the result and do some magic with it
var message = data.Message;
$("#resultMessage").html(message);
},
error: function () {
$('#message').html('oops Error').fadeIn();
}
});
return false;
});
});
</script>
<fieldset>
<legend>Fighter template</legend>
<div class="editor-label">
<%: Html.LabelFor(model => model.Id) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.Id) %>
<%: Html.ValidationMessageFor(model => model.Id) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FighterName) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FighterName) %>
<%: Html.ValidationMessageFor(model => model.FighterName) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FighterStyleDescription) %>
</div>
<div class="editor-field">
<%: Html.TextBoxFor(model => model.FighterStyleDescription) %>
<%: Html.ValidationMessageFor(model => model.FighterStyleDescription) %>
</div>
<div class="editor-label">
<%: Html.LabelFor(model => model.FighterLongDescription) %>
</div>
<div class="editor-field">
<%: Html.TextAreaFor(model => model.FighterLongDescription) %>
<%: Html.ValidationMessageFor(model => model.FighterLongDescription) %>
</div>
<p>
<input type="submit" value="Save" id="save" />
</p>
<p>
<input type="submit" value="JSONTRICK" id="JSONTRICK" />
<label id="message">
message</label>
</p>
<div>
<span id="resultMessage"></span>
</div>
</fieldset>
<% } %>
そして、これがコントローラーからの関連ビットです:
[HttpPost]
public ActionResult Edit(int id, FormCollection collection) //Works
{
var fighter = _FightDb.Fighters.Single(f => f.Id == id);
try
{
UpdateModel(fighter);
//var x = ViewData.GetModelStateErrors();
_FightDb.SaveChanges();
return RedirectToAction("Index");
}
catch
{
var viewModel = _FightDb.Fighters.Single(f => f.Id == id); //fighter;
return View(viewModel);
}
}
[HttpPost]
public ActionResult UpdateJsonTrick(Fighter fighter) //doesn't work
{
var x = ViewData.GetModelStateErrors();
string returnMessage = string.Format("Created fighter '{0}' in the system.", fighter.FighterName);
return Json(new PersonViewModel { Message = returnMessage });
}
ご理解いただきありがとうございます。追加のコードや情報が必要な場合は、提供させていただきます。
解決
JsonValueProviderFactory
がデフォルトで登録されていないMVC 3プレビュー1にはバグがあります。
あなたのGlobal.asax
にこのような何かを持つことに役立つはずます:
ValueProviderFactories.Factories.Add(new JsonValueProviderFactory())