asp.net mvc jquery $ ajax将空值传递给控制器

时间:2011-02-23 08:45:37

标签: asp.net-mvc jquery

$("#ftp-dialog").dialog({ autoOpen: false });
    $('.ftp').live("click", function(event) { loadDialog(this, event, '#ftp-dialog'); });
});
    function loadDialog(tag, event, target) {
        event.preventDefault();
        var $loading = $('<img src="../../Content/images/ajaxLoading.gif" alt="loading" class="ui-loading-icon">');
        var $url = $(tag).attr('href');
        var $title = $(tag).attr('title');
        var $dialog = $('<div></div>');
        $dialog.empty();
        $dialog
            .append($loading)
            .load($url)
            .dialog({
                autoOpen: false,
                title: $title,
                width: 300,
                modal: true,
                minHeight: 200,
                show: 'fade',
                hide: 'fade'
            });


            $dialog.dialog("option", "buttons", {
                "Cancel": function() {
                    $(this).dialog("close");
                    $(this).empty();
                },
                "Save": function() {
                    var dlg = $(this);
                    $.ajax({
                        url: $url,
                        type: 'POST',
                        data: $("#target").serialize(),
                        success: function(response) {
                            $(target).html(response);
                            dlg.dialog('close');
                            dlg.empty();
                            $("#ajaxResult").hide().html('Record saved').fadeIn(300, function() {
                                var e = this;
                                setTimeout(function() { $(e).fadeOut(400); }, 2500);
                            });
                        },
                        error: function(xhr) {
                            if (xhr.status == 400)
                                dlg.html(xhr.responseText, xhr.status);     /* display validation errors in edit dialog */
                            else
                                displayError(xhr.responseText, xhr.status); /* display other errors in separate dialog */

                        }
                    });
                }
            });


        $dialog.dialog('open');
    };

以下是视图和控制器:

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<jh.Models.editFtpViewModel>" %>
<%using (Html.BeginForm("EditFtp", "Pages", FormMethod.Post, new { id = "target" }))
  { %>
<table>
    <tr>
        <td colspan="2">
            Enter Ftp Address:
             <%=Html.TextBoxFor(x => x.ftpAddress, new {@class="text ui-widget-content ui-corner-all"})%>
        </td>
        <td>
        </td>

    </tr>
    <tr>
        <td>
            Login Name:<br/>
            <%=Html.TextBoxFor(x => x.loginName, new { @class = "text ui-widget-content ui-corner-all", style="width:120px;"})%>
        </td>
        <td>
            Password:
            <%=Html.PasswordFor(x => x.Password, new { @class = "text ui-widget-content ui-corner-all", style="width:120px;" })%>
        </td>
    </tr>
</table>
<input type="submit" id="button" value="Save" />
<%} %>

控制器:

[HttpPost]
        public ActionResult EditFtp(editFtpViewModel model)
        {
            return View();
        }

问题是传递给控制器​​的所有值都为null。但如果我做一个简单的提交,一切都还可以。有人能帮助我吗?

editFTPViewModel类:

public class editFtpViewModel
    {
        public string ftpAddress { get; set; }
        public string loginName { get; set; }
        public string Password { get; set; }
    }

我想根据此模型将表单值传递给控制器​​。

1 个答案:

答案 0 :(得分:1)

值为null是因为在调用对话框时,它会重建DOM并且MVC会丢失输入。最初调用对话框时,需要添加:

open: function () { $(this).parent().appendTo("#target"); }

到构造函数。因此,在您的情况下,它将是:

$("#ftp-dialog").dialog({ autoOpen: false, open: function () { $(this).parent().appendTo("#target"); } });