문제

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!

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top