2

古いチュートリアルとMVC3Preview 1の最近の投稿を組み合わせて一致させると、次の問題が発生します。FighterJSONを使用しない「単純な古い」編集ではなく、モデル(および基盤となるデータベース)のJSON駆動型編集に移行しようとしています。

クラス(EF 4モデルに存在する)の編集ビュー(、を使用Shared EditorTemplate)のfighter.ascxセットアップがあります。Fighter

これには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 });

}

ご理解のほどよろしくお願いいたします。追加のコードや情報が必要な場合は、提供させていただきます。

4

1 に答える 1

1

JsonValueProviderFactoryMVC 3 Preview 1 には、デフォルトで が登録されていないというバグがあります。

あなたの中にこのようなものがあれば助けになるGlobal.asaxはずです:

ValueProviderFactories.Factories.Add(new JsonValueProviderFactory())
于 2010-08-04T23:01:58.170 に答える