如何将 MVC 部分视图放入灯箱中

How to put MVC partial view into a lightbox?

本文关键字:视图 MVC      更新时间:2023-09-26

所以我有一个共享的布局。我想在该布局上有一个搜索表单。进行搜索后,我想将结果返回到灯箱中。我不是JS最伟大的。这是我所拥有的:

共享布局:

<div id="search-form">
@Html.Action("Search", "RespondentSearch")
</div>
<div id="search-results">
</div>

控制器:

[HttpGet]
[Whitelist]
public ActionResult Search()
{
     return PartialView("_SearchFormPartial");
}
[HttpPost]
[Whitelist]
public ActionResult Search(string query)
{
     return PartialView("_SearchResultsPartial");
}

搜索表单部分视图:

@using (Ajax.BeginForm("Search", "RespondentSearch", FormMethod.Post,
        new AjaxOptions
        {
            InsertionMode = InsertionMode.Replace,
            HttpMethod = "POST",
            UpdateTargetId = "search-results"
        },
        new
        {
            @class = "sidebarSearch"
        }
        ))
{
    <div>
        <input type="text" name="search" placeholder="respondent search..." id="ac" class="ui-autocomplete-input" autocomplete="off" />
        <span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span>
        <input type="submit" value="" rel="lightbox" />
    </div>
}

搜索结果部分:(这整个表格是我想在灯箱内显示的)

<table>
<tr>
<td>Stuff in Here</td>
</tr>
</table>

我修复了它:

共享视图:

        <div id="RespondentSearch">
            @Html.Action("Search", "RespondentSearch")
        </div>
        <div id="search_results" style="display: none">
        </div>

控制器:

[HttpGet]
[Whitelist]
public ActionResult Search()
{
     return PartialView("_SearchFormPartial");
}
[HttpPost]
[Whitelist]
public ActionResult Search(string query)
{
     return PartialView("_SearchResultsPartial");
}

共享视图中的表单(一切的核心)

<div class="sidebarSearch">
    <input type="text" name="query" placeholder="respondent search..." id="ac" class="ui-autocomplete-input" autocomplete="off" />
    <span role="status" aria-live="polite" class="ui-helper-hidden-accessible"></span>
    <a href="#search_results" rel="lightbox" id="performSearch"></a>
</div>
<script>
    $('#performSearch').on('click', function () {
        $.ajax({
            url: "@(Url.Action("Search", "RespondentSearch"))",
            type: 'POST',
            data: { query: $('#query').val() },
            cache: false,
            async: true,
            success: function (result) {
                $("#search_results").html(result);
            }
        });
    });
</script>