JSON / MVC (3P1) httppost - عدم الحصول عليها للعمل في فصل EF الخاص بي

StackOverflow https://stackoverflow.com/questions/3410294

سؤال

في خلط ومطابقة البرامج التعليمية القديمة مع المشاركات الحديثة على MVC 3 Preview 1 ، واجهت المشكلة التالية. أحاول الانتقال إلى التعديلات التي يحركها JSON Fighter النموذج (و DB الأساسي) بدلاً من التعديلات "القديمة القديمة" بدون JSON.

لدي عرض تحرير (يستخدم Shared EditorTemplate, fighter.ascx) الإعداد لي Fighter الفصل (الموجود في نموذج EF 4).

على هذا لديّ 2 أزرار. واحد "قديم" ، وهو إرسال يدعو تحرير My EditController بدون JSON ، وواحد جديد ، كتبته جديدًا من أجله HttpPost ActionResult

يعمل الزر القديم: الزر الجديد هو نصف تنفيذ فقط ولكن يمكنني بالفعل أن أرى أن ActionResult UpdateJsonTrick لا يتلقى البيانات من العرض بشكل صحيح. ال returnMessage تقرأ السلسلة: "تم إنشاء مقاتلة" في النظام. " قبل أن أتمكن من فعل أي شيء مفيد في هذا الإجراء ، يجب أن أعرف كيفية تمرير هذه البيانات. هل أنا على خطأ؟

لذا ، فإن edit.aspx مجرد بسيط Html.EditorForModel("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 });

}

شكرًا لصبرك ، إذا كنت بحاجة إلى رمز أو معلومات إضافية ، فيمكنني تقديمها.

هل كانت مفيدة؟

المحلول

هناك خطأ في MVC 3 معاينة 1 حيث JsonValueProviderFactory غير مسجل بشكل افتراضي.

وجود شيء مثل هذا في الخاص بك Global.asax يجب المساعدة:

ValueProviderFactories.Factories.Add(new JsonValueProviderFactory())
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top