繁体   English   中英

MVC 局部视图 Model 不刷新

[英]MVC Partial View Model Not Refreshing

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

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

这在某些情况下可以正常工作,在其他情况下,model 将需要很长时间才能加载。 关闭局部视图/模态并单击主视图上表中的另一个链接后,局部视图将加载显示先前加载的数据。 它没有正确刷新。

主视图模态定义:加载中,请稍候...

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

Controller:

    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>
    <% } %>

任何帮助深表感谢。

使用 controller 操作上的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);
}

听起来像一个缓存问题。 某些浏览器可能会缓存 GET 请求。 尝试将$.get AJAX 调用替换为$.ajax .ajax 调用,方法是设置cache: false或尝试$.post请求:

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

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

另一种方法是在使用内置 MVC 帮助程序时将随机数附加到查询字符串。

例如:

@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 自己处理提交,这是一个快速的解决方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM