从控制器更新部分视图
本文关键字:视图 更新部 控制器 | 更新日期: 2023-09-27 18:11:20
我读过一些关于如何更新局部视图的教程,但它们都假设用户交互作为起点(如下拉更改,将导致局部视图中的一些数据更新)。我的情况是不同的:我试图从控制器开始更新部分视图,没有用户交互的UI。如果我使用这样的代码:
[HttpPost]
public ActionResult PerformAction(MyModel mymodel)
{
//....do things
return PartialView("Notification", mymodel);
}
浏览器将只显示部分视图。建议吗?由于
主视图如下:
@using System.Web.UI.WebControls
@model MyApplication.Models.Mymodel
@{
ViewBag.Title = "Home Page";
}
<form class="float_left" method="post" action="@Url.Action("Start", "Home")">
<fieldset>
@Html.TextBoxFor(m => m.Username, new { Value = Model.Message })
<input type="submit" value="Subscribe"/>
<div id="notificationMessage">
@{ Html.RenderPartial("~/Views/Home/PartialView.cshtml", new PartialViewModel()); }
</div>
</fieldset>
</form>
这里是局部的:
@using System.Web.UI.WebControls
@model HostedExchangeListener.Models.PartialViewModel
@Html.TextBoxFor(m => m.Message, new { Value = Model.Message })
您需要做的是这样做:
<<p> 视图/strong><div id="partialViewContent"> </div>
<script>
$(function () // Once the DOM is loaded
{
setInterval(function(){
$.get('@Url.Action("Action","Controller")', function(data) {
$('#partialViewContent').html(data); // Replace whats in the div with the PartialView
}, 5000); // Every 5 seconds
});
</script>
控制器public ActionResult Action()
{
var mymodel = new MyViewModel();
//....do things
return PartialView("Notification", mymodel);
}