2012-07-05 93 views
1

我有MVC3 Razor視圖引擎中的視圖,如下圖所示。現在我想確認連接動作輸出顯示在這個鏈接文本下不是新頁面。我怎樣才能完成這項工作?在MVC3 Razor視圖中的局部視圖引擎

請用示例代碼解釋。

enter image description here

我查看像這樣:

@model ESimSol.BusinessObjects.COA_ChartsOfAccount 
@{ 
    ViewBag.Title = "Dynamic Account Head Configure"; 
} 

<h2>Dynamic Account Head Configure</h2> 

<table border="0"> 
    <tr> 
     <td> Select an Server Connection </td> 
     <td style="width:5px">:</td> 
     <td>@Html.DropDownListFor(m => m.DBConnections, Model.DBConnections.Select(x => new SelectListItem() { Text = x.ConnectionName, Value = x.DBConnectionID.ToString()}))</td>   
    </tr> 
    <tr> 
     <td> </td> 
     <td style="width:5px"></td> 
     <td>@Html.ActionLink("Confirm Connection", "ConformConnection")</td>   
    </tr> 
</table> 

和我的控制器動作像如下:

public ActionResult ConfirmConnection() 
     {   
      return PartialView(); 

     } 

回答

0

首先移動您的標記的局部視圖。之後定義一個呈現你的局部視圖的動作方法。

[ChildActionOnly] 
public ActionResult ConfirmConnection(COA_ChartsOfAccount model) 
{   
    return PartialView("MyPartialView", model); 
} 

ChildActionOnly屬性確保此操作方法不能被HTTP請求調用。

然後,您可以使用Html.Action方法隨時顯示它。

@Html.Action("ConfirmConnection", "MyController", new { model = Model }) 

忽略將模型作爲參數傳遞,如果它不通過顯示它的頁面進行更改。您可以在您的操作方法中檢索它。

0

我使用jQuery和Ajax對於這種事情的大風扇...... http://api.jquery.com/jQuery.ajax/

如果你遵循了典型的MVC模式,那麼你可以添加一個動作鏈接使用類似的頁面...

@Html.ActionLink("controller", "action", args); 

,但我會去爲Ajax驅動的方法...

<script type="text/javascript"> 
     var ajaxBaseUrl = '@Url.Action("yourController", "ConformConnection", new { args })'; 
     $(link).click(function() { 
      var currentElement = $(this); 
      $.ajax({ 
       url: ajaxBaseUrl, 
       data: { any other queryString stuff u want to pass }, 
       type: 'POST', 
       success: function (data) { 
         // action to take when the ajax call comes back 
        } 
       }); 
      }); 
     }); 
    </script>