在renderPartial中刷新viewData对象 [英] Refresh viewData object in renderPartial

查看:121
本文介绍了在renderPartial中刷新viewData对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此我需要帮助.

我有一个包含客户端列表的viewdata,并且在调用Index action方法时提供了它.

I have a viewdata with a list of clients and i feed it when the Index action method is called.

/*索引操作是打开视图的方法. */

/* Index action is the method that open the view. */

调用Index方法后,将输入变量并显示视图.

Once that the Index method was called, the variable is feeded and view is showed.

然后,我有一个对话框,其中呈现一个PartialView,其中有一个包含客户列表的表.

Then, i have a dialog where is render a partialView, wich have a table with the list of clients.

<div id="popupClients" class="popUp" style= "display:none">
    <% Html.RenderPartial("ClientsPartialView", ViewData["clients"]); %>

/* popupClients是对话框. */

/* popupClients is the dialog. */

/* ClientsPartialView是partialView,其中包含一个显示客户端ID和名称的表. */

/* ClientsPartialView is a partialView with a table that show id and name of clients. */

提示是如何在显示之前刷新视图数据的数据?

The cuestion is how i can refresh the data of the viewdata before being displayed?

我问这是因为,如果有人插入新客户端,则必须在partialView中显示

I ask this because, if someone insert a new client, has to be displayed in the partialView

Thnxs!

推荐答案

您可以使用 Ajax 在显示该对话框之前刷新页面中代表popupClients的部分.

You can use Ajax to refresh the portion of your page that represents the popupClients just prior to showing that dialog.

如果部分视图呈现的是包含所有弹出内容的div,则可以使用Ajax刷新div,类似于:

If your partial view renders, say, a div containing all of the content for the popup, you can use Ajax to refresh that div, something along the lines of:

function getCustomerList(searchCriteria) {
    $.ajax({
        url: 'Home/GetClientList',
        type: 'POST',
        async: false,
        data: { searchString: searchCriteria },
        success: function (result) {
            $("#popupClients").html(result);
            $( /*... do whatever you do now to show your dialog....*/ ;
        }
    });
};

更新

根据您的评论...

每当您运行服务器端代码(并且您刚刚在注释中向我显示了服务器端代码)时,您就可以获取新数据.当部分视图回调给控制器时,如果这是业务要求,则控制器应从数据库更新模型.

Every time your server-side code runs (and you just showed me server-side code in your comment), you are in a position to get fresh data.  When your partial view calls back to your controller, your controller should update the Model from the database if that is the business requirement.

类似的东西:

[OutputCache(Duration = 0)]
public ActionResult _ClientList()
{
    List<Clients> clientList = GetCurrentClientListsFromDB();

    return PartialView(clientList);
}

这将在每次调用控制器时对数据库进行检查.如果错过最近的更新是可以接受的,则可以更改 SQL依赖性,这样当基础表发生更改时,它会自动失效,而不仅仅是根据经过的时间刷新.设置起来比较复杂,但是会给出更准确的结果.

That will cause a check to the database every time the controller is invoked. If it's acceptable to miss a recent update, you can change the value of OutputCache to inform the MVC engine to cache the result for a given number of seconds. You can also configure the OutputCache to refresh based a SQL Dependency so that it is automatically invalidated when there is a change to an underlying table rather than just refreshing based on elapsed time. That's more complex to setup, but will give a more accurate result.

这篇关于在renderPartial中刷新viewData对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆