2012-10-08 41 views
2

我有一個顯示用戶評論的網頁。我默認頁面顯示最近10次。在底部,當使用點擊「顯示更多」時,下面會顯示10條評論。用戶可以多次點擊該按鈕,每次底部顯示10個註釋。問題是用戶必須再次向下滾動。我想使用錨點來選擇最後顯示的評論。如何在MVC 4中使用HTML錨?

在我看來,我有:

@{ int i = 1; } 
@foreach (var item in Model) { 
    <div class="Comment" id="[email protected]"> 
     @Html.DisplayFor(modelItem => item.CommentText) 
    </div> 

    i++; 
} 

@{int numCommentsToDisplay = 10;} 
@if (Session["numCommentsToDisplay"] != null) { 
    numCommentsToDisplay = Convert.ToInt32(Session["numCommentsToDisplay"]); 
} 

@Html.ActionLink("Show More", "Index", new { numCommentsToDisplay = numCommentsToDisplay + 10 }) 

而且我控制器包含:

public ActionResult Index(int numCommentsToDisplay = 10) { 
    this.HttpContext.Session.Add("numCommentsToDisplay", numCommentsToDisplay.ToString()); 
    var latestComments = db.Comments.Take(numCommentsToDisplay).OrderByDescending(c => c.TimeStamp).ToList(); 

    return View(latestComments); 
} 

當用戶點擊「顯示更多」,第一次,20條評論將被顯示,如何我可以選擇第11條評論嗎?我已經設定的ID和手動導航到http://localhost:49208/Comment?numCommentsToDisplay=20#Comment-11的伎倆

感謝

回答

4

我已經解決了我自己的問題wahey!

我發現了一個過載Html.ActionLink(),不只是工作:

public static MvcHtmlString ActionLink(
    this HtmlHelper htmlHelper, 
    string linkText, 
    string actionName, 
    string controllerName, 
    string protocol, 
    string hostName, 
    string fragment, 
    Object routeValues, 
    Object htmlAttributes 
) 

所以我通過我的主播片段:

@Html.ActionLink("Show More", 
       "Index", 
       "Comment", 
       null, 
       null, 
       String.Format("Comment-{0}", numCommentsToDisplay + 1), 
       new { numCommentsToDisplay = numCommentsToDisplay + 10}, 
       null 
       ) 

我在這裏找到了答案:Including an anchor tag in an ASP.NET MVC Html.ActionLink

非常感謝您的輸入

0
@Html.ActionLink(
     "Show More",       
     "Index",      
     new { numCommentsToDisplay = numCommentsToDisplay + 10 },   
     new { name = string.Format("Comment-{0}",numCommentsToDisplay) }  
    ) 
+0

我無法得到您的答案。這是通過HtmlAttributes?哪個不會通過我的錨?或者我錯過了什麼? – Rodders

+1

請向您的代碼片段添加一些解釋。 –

0

我會建議使用一些腳本,客戶端大小。

您使用的是MVC4,我的建議是利用WebApi。 設置api控制器以返回分頁的註釋並使用Knockout.js將結果呈現在頁面中。

+0

我以前沒有嘗試過的東西,現在我已經解決了我的問題,但是當我獲得時間時會檢查您的建議。謝謝 – Rodders

+1

這可能是一個不錯的額外功能,但如果頁面沒有JavaScript的話,它幾乎總是很好。 – andrewf