【问题标题】:How to avoid page refresh in asp.net mvc如何避免在asp.net mvc中刷新页面
【发布时间】:2019-01-23 19:46:16
【问题描述】:

without refresh

当我单击局部视图的任何页面链接时,该相关页面应显示在渲染正文部分中,而无需任何页面刷新。我该怎么做?

【问题讨论】:

  • 你可以为它做ajax调用。
  • 您使用的是哪个版本的 MVC? .NET4.6 还是 Core?
  • 实际上,这个话题太宽泛了,示例代码只能让您了解这么多。这是documentation on the topic 让您继续前进。无所谓 Asp.Net 的风格。 Hth

标签: asp.net-mvc model-view-controller


【解决方案1】:

您可以使用与不显眼的 ajax 结合使用的 AJAX 助手

You can find more information at this page

  • 安装Microsoft.jQuery.Unobtrusive.Ajax NuGet 包
  • 在 _Layout <script src="~/Scripts/jquery.unobtrusive-ajax.min.js"></script> 中包含脚本
  • 然后您可以使用带有特定选项的 HTML Helper

代码示例

@Ajax.ActionLink("View  All Student Info", "AllStudent", "Home", new AjaxOptions  
{  
  UpdateTargetId = "divAllStudent", 
  OnBegin = "fnOnBegin",  
  InsertionMode = InsertionMode.Replace,  
  HttpMethod = "GET",  
  LoadingElementId = "imgloader",  
  OnSuccess= "fnSuccess",  
  Confirm="Do you want to get all student info ?????"  
}, 
new { @class = "btn btn-default" })
  • 然后在控制器中添加一个特定的 [GET] Route (WebAPI)

代码示例

[HttpGet]  
public PartialViewResult AllStudent()  
{  
    using (TempEntities db = new TempEntities())  
    {  
        var objAllStudent = db.StudentInfoes.ToList();  
        return PartialView("AllStudent", objAllStudent);  
    }  
}

选项UpdateTargetId 是AJAX 结果将放置结果内容的HTML ID 容器。通常你想使用替换。您可以使用 OnBegin 和 OnSuccess 作为 Javascript 方法来执行显示加载器、隐藏加载器等操作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-04
    • 2023-03-13
    • 2020-02-12
    • 2017-05-17
    • 2013-02-03
    • 1970-01-01
    相关资源
    最近更新 更多