2011-03-17 35 views
0

打開一個jQuery對話框我有一個aspx頁面和用戶控件[它包含一個按鈕,文本框] 和用戶控件添加到aspx頁面如何在MVC

所以,當我在aspx頁面點擊按鈕,它應該作出Jquery ajax調用Controller來打開一個對話框。

我該如何做到這一點....在這我們必須撥打電話給控制器

+0

有東西在這裏:HTTP://計算器。 COM /問題/ 8541821 /如何對簡化 - 我 - 有狀態的隔行模態的對話框,在-ASP淨MVC – 2012-09-11 14:35:35

回答

1

這是很容易 - 創建partialview,這將是實際的對話內容(姑且稱之爲MyDialogPV) - 創建在返回PartialView(GetMyDialogPV)控制器的方法 - 您使用AJAX來調用PartialView並在運行時將其渲染爲對話框(在我使用JqueryUI插件的示例中)。

控制器代碼:

public PartialViewResult GetMyDialogPV() { 
     //do some stuff, probably you'll need to set up a model given input parameters 
     ViewBag.Text = "This is my dialog text, opened at " + DateTime.Now.ToLongTimeString(); 
     //retunr the partialview rendered 
     return PartialView("MyDialogPV"); 
    } 

的對話框中的部分觀點:

<div>Title</div> 
<div> 
    @ViewBag.Text 
</div> 

父頁面代碼:

@{ViewBag.Title = "Home Page";} 
<script> 
$(document).ready(function() { 
    $('#lod').click(function() { //click event of the link    
     //load the dialog via ajax 
     $.ajax({ 
      url: '@(Url.Action("GetMyDialogPV"))', 
      type: "GET", 
      dataType: "html", 
      cache: false, 
      success: function (data) { 
       $('#myDialogContainer').html(data); //write the dialog content into the diaog container 
       $("#myDialogContainer").dialog({ //dialogize it with JqueryUI 
        autoOpen: false, 
        height: 400, 
        width: 450, 
        modal: true, 
        buttons: { 
         "Chiudi": function() { $(this).dialog("close"); } 
        }, 
        close: function() { } 
       }); 
       $("#myDialogContainer").dialog("open"); //open it! 
      }, 
      error: function (xhr, ajaxOptions, thrownError) { 
       alert('Error'); 
      } 
     }); 
    }); 
}); 
</script> 
<a id="lod">Load And Open Dialog</a> 
<div id='myDialogContainer' style="background:red">this is the container for the html retrieved by the controller action GetMyDialogPV</div>