1

I'm trying to populate one of my unordered list () with items from my DB automatically just as the page has finished loading up. What I need to know is how to get the items. I don't think anything is wrong with the Controller and PartialView. All I need is how to call it from the index page.

The Following code is from the index page:

<div class="contentRight">
            <span class="contentRightHeader">Most Followed Questions ·</span><span class="contentRightViewAll"> View all</span>
            <ul>
                <!--<li>Why do we drink and whats the effects on the body</li>
                <li>Why do we drink and whats the effects on the body</li>-->
            </ul>
        </div>

This code is from the controller:

public PartialViewResult _ListMostFollowedQuestions()
    {
        QuestionManager qman = new QuestionManager();
        ViewBag.Questions = qman.ListMostFollowedQuestions(3);
        return PartialView("_ListMostFollowedQuestions");
    }

And lastly from my partialView:

@foreach (var item in ViewBag.Questions)
{
@item.Topic Deadline:
    @item.Deadline      
}

Thanks for the help!

1
  • try using Ajax to load your list after loading index page Commented Jul 25, 2011 at 10:50

1 Answer 1

1

you can do it in two ways first in your partial view construct the html and call it inside the ul like

@foreach (var item in ViewBag.Questions)
{
"<li>"[email protected] Deadline:@item.Deadline+"</li>"
}

and in

<div class="contentRight">
            <span class="contentRightHeader">Most Followed Questions ·</span><span class="contentRightViewAll"> View all</span>
            <ul>
               @Html.RenderPartial("_ListMostFollowedQuestions")
            </ul>
        </div>

the second way is the ajax way in your index page

$(function(){

$.ajax({
type:'POST',
url:'/path/to/partial',
dataType:'html',
success:function(data){
$("span.contentRightHeader ul").append(data);
}

});

});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.