I am using MVC 3 to develop a website and when user select prefer language at homepage select list, the following code will handle the event. it will read the value of the select list and save it to cookies. Finally, I will submit the form in order to postback the page.
$(document).ready(function () {
$('#lang').change(function () {
var languageId = $('#lang').val();
document.cookie = 'culture=' + languageId;
$('#postback').submit();
});
});
the problem now is the browser keep warning me when I reload the page.
the chrome says:
The page that you’re looking for used information that you entered.
Returning to that page might cause any action you took to be repeated.
Do you want to continue?
the firefox says:
To display this page, Firefox must send information that
will repeat any action (such as a search or order confirmation)
that was performed earlier.
what is the reason? and how to solve it?
EDIT
public ViewResult Index()
{
int languageId = LocalizationHelper.TryGetCookiesLanguageId();
LoginPageViewModel model = new LoginPageViewModel
{
LanguageId = languageId,
Languages = Mapper.Map<IEnumerable<SvcLanguageList>, IEnumerable<Language>>(new[]{
new SvcLanguageList{ LanguageId = 1, LanguageName="English" },
new SvcLanguageList{ LanguageId = 2, LanguageName="简体中文" },// Simplified Chinese
new SvcLanguageList{ LanguageId = 3, LanguageName="繁體中文" },// Traditional Chinese
})
};
return View(model);
}
EDIT 2
I have changed my jquery code to the follwing:
$(document).ready(function () {
$('#lang').change(function () {
$('img#loading').show(800);
var culture = $('#lang').val();
$.ajax({
url: '@Url.Action("ChangeCulture", "Account")',
type: 'POST',
data: JSON.stringify({ culture: culture }),
datatype: 'json',
contentType: 'application/json;charset=ascii',
success: function (data) {
window.location = '/';
}
});
});
});
and here is my action in controller
[HttpPost]
public ActionResult ChangeCulture(string culture, string returnUrl)
{
Session["CultureInfo"] = new CultureInfo(culture);
Response.Cookies["culture"].Value = culture;
Response.Cookies["culture"].Expires = DateTime.UtcNow.AddYears(1);
if (Request.IsAjaxRequest())
{
return new EmptyResult();
}
else
{
return Redirect(returnUrl);
}
}
When you submit a form, you get redirected to a new page, which was
POSTed the contents of the previous page. That page would not exist without the contents of the form that wasPOSTed. Therefore trying to return to that page would need the same data re-POSTed to it.To solve this, you would need to overcome
POSTing to that page, most likely with Ajax requests.