2012-10-08 10 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

나는 내 자신의 질문을 해결했습니다. Including an anchor tag in an ASP.NET MVC Html.ActionLink

: 여기 해답을 발견

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

:

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()에 대한 과부하를 발견

귀하의 의견을 모두 주셔서 감사합니다

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

나는 귀하의 답변을 얻을 수 없습니다. 그것은 HtmlAttributes에서 전달됩니까? 내 앵커에서 어느 쪽이지나 가지 않겠습니까? 또는 나는 무엇인가 놓치고 있냐? – Rodders

+1

코드 스 니펫에 몇 가지 설명을 추가하십시오. –

0

좀 스크립팅 클라이언트 크기를 사용하여 제안 트릭을 수행했다.

MVC4를 사용하고 있는데, 제 생각에는 WebApi를 활용하는 것입니다. 페이징 된 주석을 반환하고 페이지에 Knockout.js를 사용하여 결과를 렌더링하도록 API 컨트롤러를 설정하십시오.

+0

내가 전에 해본 적이없는 것, 지금 당장 내 문제를 해결했지만 시간이 들었을 때 제안을 확인합니다. 감사합니다 – Rodders

+1

그건 좋은 추가 수도 있지만, 페이지가 JavaScript 없이도 작동 할 수 있다면 거의 항상 좋은 것입니다. – andrewf

관련 문제