ASP.NET MVC Session State

笑着哭i 提交于 2019-12-03 09:51:19

问题


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!!


回答1:


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.



来源:https://stackoverflow.com/questions/329383/asp-net-mvc-session-state

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