asp.net – 在jQuery $.post上缓存MVC 4 PartialViewResult

我有一个jQuery $.post回到MVC 4控制器,它将返回一个使用POST中发送的数据渲染的PartialViewResult.调试部分视图和控制器时,正在接收正确的数据并将其作为视图模型发送到部分视图.问题是,当分析在 AJAX结果中发回的 HTML时,它包含来自原始页面刷新的看似“缓存”的数据.

在这里看到了很多类似的帖子,但没有一个与我的问题相同.

我知道HTTP Post请求不会在浏览器中缓存,所以这不是问题.我还将OutputCache属性的设置设置为NoStore = true等.

调节器

[OutputCache(NoStore = true,Duration = 0,varyByParam = "*")]
public partial class MyController : Controller
{ 
  ...
    [HttpPost]
    public virtual ActionResult UpdatePartial(Myviewmodel myVm)
    {
        return this.PartialView("My/_Partial",myVm);
    }
}

JS

$('.someButton').click(function () {
        $.post(myAjaxUrl,$('form').serialize(),function (data) {
            $('#myContent').html(data);
        });
    });

解决方法

我可以通过在对模型执行任何操作之前添加ModelState.Clear来解决此问题.

[HttpPost] 
public virtual ActionResult UpdatePartial(Personviewmodel model) 
{ 
    ModelState.Clear(); 
    model.FirstName += "1"; 
    model.LastName += "1"; 
    model.Age += 1; 
    return this.PartialView("../My/_Partial",model); 
}

This问题由Tim Scott提供了更多信息链接.

相关文章

### 创建一个gRPC服务项目(grpc服务端)和一个 webapi项目(...
一、SiganlR 使用的协议类型 1.websocket即时通讯协议 2.Ser...
.Net 6 WebApi 项目 在Linux系统上 打包成Docker镜像,发布为...
一、 PD简介PowerDesigner 是一个集所有现代建模技术于一身的...
一、存储过程 存储过程就像数据库中运行的方法(函数) 优点:...
一、Ueditor的下载 1、百度编辑器下载地址:http://ueditor....