ASP.NET MVC Session State

本秂侑毒 提交于 2019-12-03 00:23:19

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.

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