【问题标题】:Load Partial View when document is ready文档准备好时加载局部视图
【发布时间】:2011-10-12 11:53:33
【问题描述】:

我正在尝试在页面完成加载时自动使用我的数据库中的项目填充我的无序列表 () 之一。我需要知道的是如何获得这些物品。我不认为 Controller 和 PartialView 有什么问题。我所需要的只是如何从索引页面调用它。

以下代码来自索引页面:

<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>

此代码来自控制器:

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

最后来自我的部分视图:

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

感谢您的帮助!

【问题讨论】:

  • 在加载index 页面后尝试使用Ajax 加载您的列表

标签: jquery ajax asp.net-mvc model-view-controller partial-views


【解决方案1】:

您可以通过两种方式进行操作,首先在局部视图中构造 html 并在 ul 中调用它

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

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

第二种方式是索引页面中的ajax方式

$(function(){

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

});

});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-06-29
    • 2015-07-12
    • 1970-01-01
    • 1970-01-01
    • 2011-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多