2016-06-10 65 views
0

我有像下面的控制器行動方法將返回來自數據庫的所有細節。如何在MVC中的鏈接點擊主視圖中渲染局部視圖..?

public ActionResult Index() 
    { 
     BusDataContext db = new BusDataContext(); 
     List<BusDetails> buses = db.Bus.ToList(); 
     return View(buses); 
    } 

這將返回詳細列表到主視圖,這是強類型的視圖,如下所示。

@model IEnumerable<MVC_DAL.BusDetails> 
    <p> 
    @Html.ActionLink("Create New", "AddBus") 
    </p> 
<table class="table"> 
<tr> 
    <th> 
     @Html.DisplayNameFor(model => model.BusSrlNo) 
    </th> 
    <th> 
     @Html.DisplayNameFor(model => model.BusName) 
    </th> 
    <th></th> 
</tr> 

@foreach (var item in Model) { 
<tr> 
    <td> 
     @Html.DisplayFor(modelIte => item.BusSrlNo) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.BusName) 
    </td> 
    <td> 
     @Html.ActionLink("Edit", "Edit", new { id=item.BusSrlNo }) | 
     @*@Html.ActionLink("Details", "Details", new { id = item.BusSrlNo }) |*@ 
     @Ajax.ActionLink("Details", "Details", new { id = item.BusSrlNo }, new AjaxOptions{UpdateTargetId="update" }) 
     @Html.ActionLink("Delete", "Delete", new { id = item.BusSrlNo }) 

     </td> 
    </tr> 
} 

</table> 

    <div id="update"> 

    </div> 

隨着模型類如

public class BusDetails 
{ 
    public int BusSrlNo { get; set; } 
    public string BusName { get; set; } 
} 

和細節ActionMethod由

public PartialViewResult Details(int id) 
    { 
     BusDataContext detail = new BusDataContext();  
     BusDetails bsdtledit = detail.Get_Edit(id); 
     return PartialView("Details",bsdtledit); 

    } 

通訊與上述模型類局部視圖低於:

@model MVC_DAL.BusDetails 

<div> 
    <h4>BusDetails</h4> 
    <hr /> 
    <dl class="dl-horizontal"> 
    <dt> 
     @Html.DisplayNameFor(model => model.BusSrlNo) 
    </dt> 

    <dd> 
     @Html.DisplayFor(model => model.BusSrlNo) 
    </dd> 

    <dt> 
     @Html.DisplayNameFor(model => model.BusName) 
    </dt> 

    <dd> 
     @Html.DisplayFor(model => model.BusName) 
    </dd> 

    </dl> 
</div> 
<p> 
    @Html.ActionLink("Edit", "Edit", new { /* id = Model.PrimaryKey */ }) | 
    @Html.ActionLink("Back to List", "Index") 
</p> 

所以,總體來說,我需要要在主視圖中顯示上面的局部視圖,只有在單擊主視圖中的詳細信息鏈接動作後纔會顯示上述局部視圖。以上代碼將單獨顯示不帶主視圖的細節,而不是在div(id =「update」)部分的相同頁面中更新。 那麼我如何更新相同的主視圖..?

+1

你聽說過阿賈克斯的? –

+0

@ EhsanSajjad - 我試着按照Brian和Christos指定的方式。但是沒有用.. – Abhi

回答

2

這就是微軟推出Ajax.ActionLink的地方。這是一種選擇;我個人比較喜歡jQuery的更好,所以我這樣做:

$.ajax({ 

    type: "get", 
    url: "@Url.Action("Partial", "Controller"), 
    success: function(d) { 
    /* d is the HTML of the returned response */ 
    $("#SomeParentIDForSection").html(d); //replaces previous HTML with action 
    } 
}); 

只要確保你的控制器返回的局部視圖:

public ActionResult Partial() 
{ 
    var model = .. 
    //init work 

    return PartialView(model); 
} 

更多信息請參見與這些引用一些其他的方法:

+0

我仍然沒有得到...我的ajax就像下面這樣。 $(function(){('。js-bus-details-link')。click(function(){.ajax({type:「GET」 url:「@ Url。Action(「Details」,「Home」) success:function(e){(「#update」)。html(e); } }); });但仍然只在新頁面中打開。這裏#update是僅在主視圖中的目標div。 – Abhi

+0

是js-bus-details-link超鏈接嗎?您必須通過將處理程序更改爲'click(function(arg){',然後調用'e.preventDefault();' –

+0

)來防止默認行爲。您不希望超鏈接重定向;因此preventDefault應該執行此操作或使用#而不是在HREF –

0

假設你會告訴你在下面的div局部視圖:

<div class="js-bus-details"> 
</div> 

你可以做一個Ajax調用和更新這個div裏面的HTML。這可以完成如下:

@Html.ActionLink("Details", "Details", new { id = item.BusSrlNo, @class="js-bus-details-link" }) 

然後將下面的腳本放在頁面的底部。

<script> 
$(function(){ 
    $(".js-bus-datails-link").click(function(){ 
     var busId = $(this).data("id"); 
     $.ajax({ 
      method: "GET" 
      url: "", // Here you have to place the relative url to your action 
      data: { id: busId } 
      cache: false 
      success: function(e){ 
       $(".js-bus-details").html(e); 
      } 
     }); 
    }) 
}) 
</script> 
+0

仍然我無法做到這一點。它僅在新頁面中打開..不在同一頁面中更新... – Abhi

+0

並且其重定向的URL爲'http:// localhost:55138/Home/Details/3?class = js-bus-details-link'其中'3'是我點擊的文本的標識。 – Abhi

+0

甚至斷點我放在上面腳本標籤內,這是不會在執行過程中擊中...它的給出錯誤,如斷點當前不會被擊中。調試器目標代碼類型的可執行代碼是否與此行關聯..plz答覆 – Abhi

0

通過點擊事件在javascript:

$("#myCodes").load('@Url.Action("PartialViewResult", "Controller_Name")'); 

N.B:PartialViewResult必須返回PartialView(model);.cshtml頁面的部分頁面