简体   繁体   English

通过ajax更新的局部视图不会保留数据

[英]Partial view updated via ajax does not persist the data

in a MVC 5 project, the Index page contains 2 partial views. 在MVC 5项目中,“索引”页面包含2个局部视图。 One to enter the search arguments and the other to show the rates (via ajax call). 一个输入搜索参数,另一个显示速率(通过ajax调用)。 Everything works as expected and the rates view does show the returned data. 一切都按预期工作,并且rate视图确实显示了返回的数据。 What happens is that if the user goes to another page, in the same project, then returns, the rates partial view is empty, although the search arguments in the other partial view are still present. 发生的情况是,如果用户转到同一项目中的另一个页面,然后返回,则费率部分视图为空,尽管其他部分视图中的搜索参数仍然存在。 Is there a way to persist the rates data? 有没有办法保留费率数据?

// This script is in the index page with the 2 partial views //此脚本位于带有2个局部视图的索引页面中

 <script> $(function () { $('#viewSearch').submit(function () { if ($(this).valid()) { $.ajax({ url: '/Home/GetRates', type: "POST", cache: true, dataType: 'html', data: $("#viewSearch").serialize(), success: function (data) { $('#viewRates').html(data); }, }); } return false; }); }); </script> 

// This is the viewRates //这是viewRates

@model Models.Rate @model Models.Rate

<table class="table table-bordered input-sm">

    @foreach (var item in Model)
    {
        <tr>
            <td>
                @Html.DisplayFor(modelItem => item.Notes)
            </td>
            <td>
                @Html.DisplayFor(modelItem => item.Daily)
            </td>
            <td>
                @Html.ActionLink("BOOK NOW", "Edit", new { id=item.Id })
            </td>
        </tr>   
    }

</table>

// and this is the controller //这是控制器

    [HttpPost]
[ValidateAntiForgeryToken]
public ActionResult GetRates()
{
    if (ModelState.IsValid)
    {
       // code
       ...

       return PartialView("~/Views/Shared/Rates.cshtml", rates);
    }

    return PartialView("~/Home/Contact.cshtml");
}

如果搜索结果仍然存在,则只需在文档上重新加载部分文档。

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

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