【问题标题】:Cannot Serialize datas of format Json from a Jquery Dialog to Asp.net MVC 3 controller无法从 Jquery Dialog 将 Json 格式的数据序列化到 Asp.net MVC 3 控制器
【发布时间】:2012-04-04 18:52:20
【问题描述】:

我有一个我无法弄清楚的新手问题,我有一个从 asp.net 控制器的部分视图生成的表单,然后显示在 jquery 对话框中。

我在对话框上有 2 个按钮(保存/取消)

在保存按钮上,我想序列化表单中的输入以将其发送回 Asp.net mvc actionController,但是它似乎不起作用,actioncontroller 没有从 Jquery Dialog 获取模型对象,我正在为表单使用 Jquery Serialize 函数。

这是脚本代码:

<button id="btnDialog">Account Logon</button>

<div id="Logonform"></div>

<script type="text/javascript">

    $(document).ready(function () {

        $.validator.unobtrusive.parse("#Logonform");

        $("#Logonform").dialog({
            autoOpen: false,
            modal: true,
            title: 'Login',

            buttons: {
                Save: function () {
                    alert($("#Logonform").serializeArray());
                    alert($("#Logonform").attr('UserName'));

                    $.ajax({
                        url: "@Url.Action("LogOn", "Account")",
                        type: "POST",
                        data: $("#Logonform").serialize(),
                        datatype: "JSON",
                        success: function (result) {
                            $("#Logonform").html(result).dialog('open');
                        }
                    });
                },

                Close: function () {
                    $(this).dialog('close');
                }
            }

        });

        $("#btnDialog").click(function () {
            $.ajax({
                url: "@Url.Action("LogOn", "Account")",
                type: "GET",
                success: function (result) {
                    $("#Logonform").html(result).dialog('open');
                }

                });
        });
    })

</script>

控制器:

    public ActionResult LogOn()
    {
        if (Request.IsAjaxRequest())
        {
            return PartialView("_Logon");

        }
        return View();
    }

    [HttpPost]
    public ActionResult LogOn(LogOnModel model)
    {
        if (ModelState.IsValid)
        {
            if (Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                return Json( new { result = "ok", user = model.UserName });
            }
            else
            {
                return PartialView("_Logon");
            }
        }

观点:

@model JqueryDialogTest.Models.LogOnModel


<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Models/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

@Html.ValidationSummary(true, "Échec de la connexion. Corrigez les erreurs et réessayez.")

@using (Html.BeginForm()) {
    <div>
        <fieldset>
            <div class="editor-label">
                @Html.LabelFor(m => m.UserName)
            </div>
            <div class="editor-field">
                @Html.TextBoxFor(m => m.UserName)
                @Html.ValidationMessageFor(m => m.UserName)
            </div>

            <div class="editor-label">
                @Html.LabelFor(m => m.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(m => m.Password)
                @Html.ValidationMessageFor(m => m.Password)
            </div>

            <div class="editor-label">
                @Html.CheckBoxFor(m => m.RememberMe)
                @Html.LabelFor(m => m.RememberMe)
            </div>

        </fieldset>
    </div>
}

如果您有任何评论指出错误之处,将不胜感激

干杯

【问题讨论】:

    标签: json asp.net-mvc-3 jquery-ui dialog


    【解决方案1】:

    尝试将 Logon 操作参数从 LogonModel 类型更改为字符串,并使用 JavascriptSerializer 在操作方法中自行反序列化它。如果这仍然不起作用,请使用Fiddler 查看在 POST 中发送了什么(如果有的话)。

    【讨论】:

    • 嗨,感谢您的评论,您的建议是一种方法,但是,我将脚本从 data: $("#Logonform").serialize() 更改为 data: $(' form').serialize(),它工作得很好,现在 actionController 正确获取了对象,但现在我遇到另一个问题,在将 json 表单发送到 actionController 方法之前,对话框上没有客户端验证,猜猜怎么做这样做?
    • 由于您使用 JQuery 进行 AJAX 调用,我将使用 JQuery 进行验证。这是一些关于验证的 JQery 文档 (docs.jquery.com/Plugins/Validation)。
    猜你喜欢
    • 2021-08-23
    • 1970-01-01
    • 2011-12-30
    • 2012-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多