简体   繁体   English

在ASP .NET MVC 4中动态呈现部分视图

[英]Dynamically rendering a partial view in ASP .NET MVC 4

I'm busy investigating possibilities for the following: 我正忙于研究以下方面的可能性:

I have a 'Search' view. 我有一个“搜索”视图。 I have a 3 entities, Person, Car and House. 我有3个实体,人,汽车和房屋。 I want to use this Search screen to dynamically build the search criteria based on the entity type selected by the user. 我想使用此搜索屏幕根据用户选择的实体类型动态构建搜索条件。

If the user wants to search for 'Person', then I want to dynamically return a partial view for Person specific search criteria, like Name. 如果用户要搜索“人员”,那么我想动态返回针对人员特定搜索条件(例如姓名)的部分视图。 Or if they selected Car, it returns a different partial view for search criteria of 'Engine Size', etc. 或者,如果他们选择了Car,则它将返回不同的局部视图,以查找“引擎尺寸”等搜索条件。

How do achieve this? 如何做到这一点? If I could return the partial view via an ajax call to the controller method (to avoid the postback), that'll be a huge bonus. 如果我可以通过对控制器方法的ajax调用返回部分视图(以避免回发),那将是巨大的收获。

First render all your partials in their very own div like so 首先,像这样将所有部分文件渲染到自己的div中

<div id="personSearch">
    @Html.Partial("PartialView1")
</div>

<div id="carSearch">
    @Html.Partial("PartialView1")
</div>

<div id="houseSearch">
    @Html.Partial("PartialView1")
</div>

jQuery code, we first off hide all 3 of those filters, then we can dynamically should the div's depending on the criteria selected by the user jQuery代码,我们首先隐藏所有这三个过滤器,然后我们可以根据用户选择的条件动态地使用div

So something like so 所以像这样

$(function()
{
    // Hide all divs
    $('#personSearch').hide();
    $('#carSearch').hide();
    $('#houseSearch').hide();

    // This is the ID of a dropdownlist for Person, Car or House selection
    $('#UserCriteria').change(function ()
    {
        var value = $(this).val();

        // 1 id the int id for Person
        if(value == 1)
        {
            $('#personSearch').slideDown();
            $('#carSearch').slideUp();
            $('#houseSearch').slideUp();
        }
        // 2 id the int id for Car
        if(value == 2)
        {
            $('#personSearch').slideUp();
            $('#carSearch').slideDown();
            $('#houseSearch').slideUp();
        }
        // 3 id the int id for House
        if(value == 3)
        {
            $('#personSearch').slideUp();
            $('#carSearch').slideUp();
            $('#houseSearch').slideDown();
        }
    }.change();
});

So what we are really doing here, is basically rendering all partial views on to the page, however using the .change() on our $('#UserCriteria') if the default value is null nothing will happen, however if the default value is "Person" (id = 1). 因此,我们实际上在这里所做的基本上是将所有部分视图呈现到页面上,但是如果我们的$('#UserCriteria')使用.change()如果默认值为null,则什么都不会发生,但是如果默认值是“人”(id = 1)。 the personSearch div will slideDown and will display your Partial view. personSearch div将向下滑动并显示您的部分视图。

I hope this is what you were looking for :) 我希望这就是你想要的:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM