从控制器更新局部视图

时间:2015-09-04 08:03:04

标签: c# asp.net-mvc model-view-controller partial-views

我已经阅读了一些关于如何更新局部视图的教程,但他们都假设用户交互作为起点(如下拉式更改将导致部分视图中的某些数据更新)。我的情况不同:我正在尝试从控制器开始更新部分视图,而无需用户在UI上进行交互。如果我使用这样的代码:

�� sr Main$Bar+a!N��b L at Ljava/lang/String;L bq ~ xpt abcdeft xyz

浏览器仅显示部分视图。建议?感谢

以下是主要观点:

    [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>

1 个答案:

答案 0 :(得分:0)

你需要做的是这些方面:

查看

<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);
}