简体   繁体   English

MVC 局部视图 Model 不刷新

[英]MVC Partial View Model Not Refreshing

I have a partial view which is being loaded into an jQuery modal in asp.net MVC 3. The problem is that the view is not refreshing properly.我有一个部分视图正在加载到 asp.net MVC 3 中的 jQuery 模态中。问题是视图没有正确刷新。 Here is the order of events:以下是事件顺序:

1) The main view has a table listing different events records. 1) 主视图有一个表格,列出了不同的事件记录。 There is a link on each row of the table to show the event detail.表格的每一行都有一个链接来显示事件详细信息。 2) When the link on this table is clicked, the partial view is loaded in the modal. 2)当点击此表上的链接时,在模态中加载部分视图。

This works fine in some cases, in other cases the model will take very long to load.这在某些情况下可以正常工作,在其他情况下,model 将需要很长时间才能加载。 After closing the partial view/modal and clicking on another link from the table on the main view, the partial view will load showing the data from the previous load.关闭局部视图/模态并单击主视图上表中的另一个链接后,局部视图将加载显示先前加载的数据。 It is not refreshing correctly.它没有正确刷新。

Definition of Modal on Main View: Loading, please wait...主视图模态定义:加载中,请稍候...

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

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

Partial View:局部视图:

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

Any help is much appreciated.任何帮助深表感谢。

Use the OutputCacheAttribute on your controller action to disable caching.使用 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);
}

Sounds like a caching issue.听起来像一个缓存问题。 GET requests could be cached by some browsers.某些浏览器可能会缓存 GET 请求。 Try replacing the $.get AJAX call by a $.ajax by setting cache: false or try a $.post request:尝试将$.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);
    }
});

Also you don't need clearing the ModelState in your EventRegistrantSummary action as you are not modifying any of the values.此外,您不需要在EventRegistrantSummary操作中清除 ModelState,因为您没有修改任何值。

Another approach is to attatch a random number to the query string as you are using the in build MVC helpers.另一种方法是在使用内置 MVC 帮助程序时将随机数附加到查询字符串。

For example:例如:

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

Or for a form:或者对于一个表格:

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

Not always the best way to do things but if you want to avoid tagging each of your methods (If you have alot that need to avoid cache problems) or using jQuery to handle the submission yourself this is a quick work around.并非总是最好的方法,但如果您想避免标记每个方法(如果您有很多需要避免缓存问题)或使用 jQuery 自己处理提交,这是一个快速的解决方法。

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

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