MVC部分视图隐藏主视图

时间:2012-04-18 11:45:33

标签: asp.net-mvc-3 partial-views

我是MVC的新手。使用MVC 3。

我有如下的PersonMaster控制器......有以下两个操作。

public ActionResult TopTenPersons()
    {
        Thread.Sleep(2000);
        var persons = DatabaseHelper.Instance.Database.Query<Person_Master>("select top(10) from person_master").ToList();
        return View("_PersonGrid", persons);
    }

    public ActionResult Index()
    {
        var persons = DatabaseHelper.Instance.Database.Query<Person_Master>("select * from person_master").ToList();

        return View("_PersonGrid",persons);
        //return View();
    }

然后,index.cshtml视图如下..

@model System.Collections.Generic.ICollection<MyPracticeApp1.Models.Person_Master>
@section scripts{
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript">    </script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
}

<h2>Persons</h2>

<p>

@Ajax.ActionLink("Click to top 10 Persons","TopTenPersons", new AjaxOptions{
UpdateTargetId="tbl_person",
InsertionMode=InsertionMode.Replace,
HttpMethod="GET"
})
</p>
<div id="tbl_person">
@Html.Action("Index","PersonMaster");
</div>
在索引视图中上面调用的

..和部分视图_PersonGrid.cshtml如下...

@model System.Collections.Generic.ICollection<MyPracticeApp1.Models.Person_Master>
@{
var persongrid = new WebGrid(Model);
}
@persongrid.GetHtml()

....但问题是它直接显示局部视图。它没有渲染索引视图的Ajax Actionlink。那么,错误在哪里?

2 个答案:

答案 0 :(得分:1)

更改TopTenPersons以返回Partial View,而不是View

public ActionResult Index()
{
    var persons = DatabaseHelper.Instance.Database.Query<Person_Master>("select * from person_master").ToList();

    return PartialView("_PersonGrid",persons);
}

答案 1 :(得分:1)

将layout = null添加到_PersonGrid.cshtml

好像是

@{
   Layout = null;
}

添加此代码时无法呈现完整视图..