Refresh viewData object in renderPartial

夙愿已清 提交于 2019-12-12 04:07:57

问题


I need help with this..

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. */

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

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 is the dialog. */

/* 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?

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

Thnxs!


回答1:


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

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....*/ ;
        }
    });
};

UPDATE

Based on your comment...

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.

Something along the lines of:

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

    return PartialView(clientList);
}

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.



来源:https://stackoverflow.com/questions/10110238/refresh-viewdata-object-in-renderpartial

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!