JSON / MVC(3P1)HttpPost - 没有得到它在我的EF类工作 [英] JSON / MVC (3P1) HttpPost - not getting it to work on my EF class

查看:183
本文介绍了JSON / MVC(3P1)HttpPost - 没有得到它在我的EF类工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在混合,并与MVC 3 preVIEW 1最近的职位匹配的旧教程,我碰到以下问题。我想移动到我的战士模式JSON驱动的编辑(和潜在的DB),而不是普通的老编辑没有JSON。

In mixing and matching older tutorials with recent posts on MVC 3 Preview 1, I run into the following problem. I am trying to move to JSON-driven edits of my Fighter model (and the underlying db) instead of 'plain old' edits without JSON.

我有一个编辑视图(即使用共享EditorTemplate fighter.ascx )设置为我的战士类(它存在于EF 4模型)。

I have an edit view (that uses a Shared EditorTemplate, fighter.ascx) setup for my Fighter class (which exists in an EF 4 model).

在此我有2个按钮。一个老之一,这是一个提交调用我的editcontroller没有JSON,一个是新的,为此,我写了一个新的 HttpPost的ActionResult

On this I have 2 buttons. One 'old' one, which is a submit that calls my editcontroller without JSON, and one is a new one, for which I've written a new HttpPost ActionResult

旧按钮的工作原理:新的按钮只有一半实现的,但我已经可以看到,的ActionResult UpdateJsonTrick 不是从视图中正确地接收数据。在 returnMessage 字符串写着:在系统中创建的战斗机'。之前,我可以做任何事情在的ActionResult是有用的,我得知道如何传递数据。我在哪里去了?

The old button works: the new button is only half-implemented but already I can see that the ActionResult UpdateJsonTrick is not receiving the data from the view correctly. The returnMessage string reads: "Created fighter '' in the system." Before I can do anything useful in that ActionResult, I've got to know how to pass that data. Where am I going wrong?

所以,edit.aspx只是一个简单的 Html.EditorForModel(斗士)语句,
,但这里是Fighter.ascx

So, the edit.aspx is just a simple Html.EditorForModel("Fighter") statement, but here is the 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 });

}

感谢您的耐心,如果你需要额外的code或信息,我可以提供。

Thanks for your patience, if you need additional code or information, I can supply.

推荐答案

有在其中 JsonValueProviderFactory 默认情况下未登记的MVC 3 preVIEW 1的错误。

There's a bug in MVC 3 Preview 1 where the JsonValueProviderFactory is not registered by default.

具有这样的事情你的的Global.asax 应该有所帮助:

Having something like this in your Global.asax should help:

ValueProviderFactories.Factories.Add(new JsonValueProviderFactory())

这篇关于JSON / MVC(3P1)HttpPost - 没有得到它在我的EF类工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆