views:

3293

answers:

1

Hi Guys,

I am currently trying to create an upload control with progress bar in MVC using jquery. I keep running into a problem however in that mvc doesn't work in parallel threads?

When I upload a file and show the progress during upload from JS performed through several callbacks to server, I am trying to get information about current upload state but answer from server comes back only when the file has been uploaded.

Do you know of any way to get session state queries in MVC constantly or when request is performed ?

Would really appreciate some help and I am sure others would if someone knows the answer!!

+4  A: 

You will lose your view state if you call an action that returns a View. You can pass data between actions using the TempData if you like, but that probably won't solve your problem. Sounds to me like what you want here is an action that will return a JSON element that you can call with some asynchronous javascript.

For your action you would have:

public ActionResult GetSuggestions(string searchText)
{
    return Json(new { SearchText = searchText + "completestring"});
}

And then on your form you have some asynchronous javascript using jQuery:

function startAutoComplete() {
    var searchText = $("#inputText").val();
    $.getJSON("/Search/GetSuggestions?searchText=" + searchText, null, autoCompleteResponse);
}

function autoCompleteResponse(data) {
    if (data.SearchText) {
        $("#inputText").val(data.SearchText);
        $("#inputText").select();
    }
}

This will allow you to get some information from your server without posting the form and keeping the viewstate of the client in tact.

There is a full write up of the example here that might help.

Odd