2011-02-23 58 views
1
$("#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; } 
    } 

我想根據該模型,以表格值傳遞到控制器。

+0

你可以顯示$(「#target」)。serialize()do和editFtpViewModel類。 – 2011-02-23 09:00:35

+0

我只想根據editFtpViewModel類將表單數據發送到控制器。 – Alex 2011-02-23 09:32:35

回答

1

值爲空的值是因爲調用對話框時,它會重建DOM並且MVC會丟失輸入。當您最初調用對話框,您需要添加:

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

的構造。所以你的情況是:

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

+0

夢幻般的答案。非常感謝。過去幾個小時我一直在試圖解決這個問題,並且破壞了我的頭腦。再次感謝。 – Somedeveloper 2012-01-26 15:56:16