2013-04-30 74 views
4

我有一個動作的結果,我想它顯示爲使用AJAX在我看來div標籤,這是我的作用的結果如何在視圖中顯示actionResult?

public ActionResult Details(string id) 
    { 
     var subscriber = new SubscribersModel(); 
     IEnumerable<Subscribe> list = from s in dbcontext.Subscribes select s; 
     foreach (var sb in list) 
     { 
      if (sb.cin == id) 
      { 
       subscriber.cin = sb.cin; 
       subscriber.name = sb.name; 
      } 
     } 
     return PartialView("Details",subscriber); 
    } 

,這是我的DetailsView

@model _3SDWebProject.Models.SubscribersModel 

<fieldset> 
<legend>SubscribersModel</legend> 

<div class="display-label"> 
    @Html.DisplayNameFor(model => model.name) 
</div> 
<div class="display-field"> 
    @Html.DisplayFor(model => model.name) 
</div> 

<div class="display-label"> 
    @Html.DisplayNameFor(model => model.cin) 
</div> 
<div class="display-field"> 
    @Html.DisplayFor(model => model.cin) 
</div> 
</legend> 

,這是我想告訴我的行爲結果的索引視圖:

@model IEnumerable<_3SDWebProject.Models.SubscribersModel> 
@{ 
    ViewBag.Title = "Subscribers"; 
    } 

<script> 
    function details(id) { 
    var url = '/Subscribers/Details/' + id; 
    $.ajax({ 
     url: url, 
     type: 'GET', 
     data: id, 
     success: function() { 
      $(".sidebar").html(); 
     } 
    }); 
} 

$(function() { 
    $(".details-logo").on('click', function() { 
     details($(this).attr("DtNo")); 
     alert("ok"); 
    }); 

}); 
    </script> 
    <div class="sidebar"> 
     //Here i want to show my result !!!!!!! 
    </div> 

<div class="content" style="width: 700px; margin-left: 250px; height: 545px;margin-top: -30px;"> 
<h2>Subscribers</h2> 
table class="altrowstable" id="alternatecolor"> 
<tr> 
    <th> 
     CIN 
    </th> 
    <th> 
     Name 
    </th> 
    <th> 
</tr> 
@foreach (var item in Model) { 
<tr id="[email protected]"> 
    <td> 
     @Html.DisplayFor(modelItem => item.cin) 
    </td> 
    <td> 
     @Html.DisplayFor(modelItem => item.name) 
    </td> 
</tr> 
    //this is my actionLink 
    @Html.ActionLink("Details", "Details", new { id = item.cin }, new { @class = "details-logo",@DtNo=item.cin}) 
} 

所以請如果有人有任何想法,我將非常感激。 注:當我按我的細節動作鏈接它給我的真實信息,但不是在我的側邊欄,但在這樣一個獨立的觀點:

enter image description here

回答

2

簡單地改變你的Ajax功能

$.ajax({ 
     url: url, 
     type: 'GET', 
     data: id, 
     success: function (result) { 
      $(".sidebar").html(result); 
     } 
    }); 

這將讓訂閱者詳細信息頁面,並把它添加到.sidebar

編輯:

更改您的onclick事件處理程序以停止超鏈接的默認操作。只需添加返回false以停止將您帶到新頁面的超鏈接。見下文。

$(function() { 
    $(".details-logo").on('click', function() { 
     details($(this).attr("DtNo")); 
     alert("ok"); 
     return false; 
    }); 

}); 
+0

感謝@UmairP,我更新我的問題 – Mohammadov 2013-04-30 12:40:23

+0

@Mohammadov我已經更新我的答案。 – 2013-04-30 12:54:04

+0

感謝@UmairP,現在它完美運行 – Mohammadov 2013-04-30 13:52:06

1

改變你的Ajax功能

function details(id) { 
    var url = '@Url.Action("Details", "Subscribers")'; 
    var params = { 
     id:id 
    } 
    $.ajax({ 
     url: url, 
     type: 'GET', 
     data: JSON.stringify(params), 
     success: function (result) { 
      $(".sidebar").html(result); 
     } 
    }); 
} 
+0

您需要將結果添加到'sidebar'元素中。以前的答案會做到這一點。 – 2013-04-30 12:35:20

+0

感謝@Saptal,我更新了我的問題,以及如何將結果添加到我的邊欄中? – Mohammadov 2013-04-30 12:39:22

+0

@Mohammadov,我已經更新了答案,von v指出錯誤 – Satpal 2013-04-30 12:40:43