MVC部分视图模型不刷新

时间:2011-08-11 14:39:57

标签: jquery asp.net-mvc

我有一个局部视图,它被加载到asp.net MVC 3中的jQuery模式中。问题是视图没有正确刷新。以下是事件的顺序:

1)主视图有一个列出不同事件记录的表。表格的每一行都有一个链接来显示事件详细信息。 2)单击此表上的链接时,部分视图将加载到模态中。

在某些情况下,这种方法很好,在其他情况下,模型将需要很长时间才能加载。关闭局部视图/模态并单击主视图上表中的另一个链接后,将加载部分视图,显示上一次加载的数据。它没有正确刷新。

主视图上模态的定义:              加载请稍候...      

<script type="text/javascript">
    $(document).ready(function () {
        $("#EventRegistrantSummary").dialog({
            bgiframe: true, autoOpen: false, height: 500, width: 980, resizable: false, modal: true
        });
    });
    function showEventRegistrantSummary(id) {
        $.get("/Event/EventRegistrantSummary/" + id, function (data) {
            $("#EventRegistrantSummary").html(data);
        });
        $("#EventRegistrantSummary").dialog('open'); return false;
    }
</script>

控制器:

    public PartialViewResult EventRegistrantSummary(Guid id)
    {
        ModelState.Clear();
        Event e = db.Events.Single(ev => ev.ID == id);
        return PartialView(e);
    }

部分视图

<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<model.Event>" %>
<% using (Ajax.BeginForm("EditUpdate", new AjaxOptions { UpdateTargetId="Target", InsertionMode= InsertionMode.Replace}))
       {%>

       <h6 style="text-align:center">Registration Summary: <%= Model.Name %></h6>

       <div style="float:left;width:35%">
            <fieldset id="Overview">
                <legend>Overview</legend>
                <div class="editor-label">
                    Total Registrants: <%= Model.BoatEventRegistrations.Count() %>
                </div>
            </fieldset>
           </div>
    <% } %>

非常感谢任何帮助。

3 个答案:

答案 0 :(得分:8)

使用控制器操作上的OutputCacheAttribute禁用缓存。

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
public PartialViewResult EventRegistrantSummary(Guid id)
{
    ModelState.Clear();
    Event e = db.Events.Single(ev => ev.ID == id);
    return PartialView(e);
}

答案 1 :(得分:5)

听起来像是一个缓存问题。某些浏览器可以缓存GET请求。尝试设置$.get或尝试$.ajax请求,以cache: false替换$.post AJAX呼叫:

$.ajax({
    url: '<%= Url.Action("EventRegistrantSummary", "Event") %>',
    type: 'GET',
    cache: false,
    data: { id: id },
    success: function(data) {
        $('#EventRegistrantSummary').html(data);
    }
});

此外,您不需要在EventRegistrantSummary操作中清除ModelState,因为您没有修改任何值。

答案 2 :(得分:0)

另一种方法是在使用in build MVC helper时将随机数附加到查询字符串。

例如:

@Ajax.ActionLink("LinkText", "Index", "Home", new { rnd = DateTime.Now.Ticks }, new AjaxOptions { UpdateTargetId = "main", OnSuccess = "pageloadSuccess" })

或表格:

using (Ajax.BeginForm("EditUpdate", new { rnd = DateTime.Now.Ticks }, new AjaxOptions { UpdateTargetId="Target", InsertionMode= InsertionMode.Replace}))

并不总是最好的方法,但是如果你想避免标记你的每个方法(如果你有很多需要避免缓存问题)或者使用jQuery自己处理提交,这是一个快速的解决方法。 / p>