Witajcie,
Chcę między akcjami kontrolera przesłać modelstate w formie ViewData.
Szczerze mówiąc działało mi to do pewnego czasu. Niestety zmieniłem dużo rzeczy ostatnio i ciężko mi powiedzieć co to spowodowało. Teraz dostaję taki komunikat
InvalidOperationException: The 'Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.TempDataSerializer' cannot serialize an object of type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary'.
Stos
InvalidOperationException: The 'Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.TempDataSerializer' cannot serialize an object of type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ViewDataDictionary'.
Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.TempDataSerializer.EnsureObjectCanBeSerialized(object item)
Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.TempDataSerializer.Serialize(IDictionary<string, object> values)
Microsoft.AspNetCore.Mvc.ViewFeatures.CookieTempDataProvider.SaveTempData(HttpContext context, IDictionary<string, object> values)
Microsoft.AspNetCore.Mvc.ViewFeatures.TempDataDictionary.Save()
Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.SaveTempData(IActionResult result, ITempDataDictionaryFactory factory, IList<IFilterMetadata> filters, HttpContext httpContext)
Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.SaveTempDataFilter.OnResultExecuted(ResultExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.ResultNext<TFilter, TFilterAsync>(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeResultFilters()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeNextResourceFilter()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Rethrow(ResourceExecutedContext context)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.Next(ref State next, ref Scope scope, ref object state, ref bool isCompleted)
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeFilterPipelineAsync()
Microsoft.AspNetCore.Mvc.Internal.ResourceInvoker.InvokeAsync()
Microsoft.AspNetCore.Builder.RouterMiddleware.Invoke(HttpContext httpContext)
Microsoft.AspNetCore.Authentication.AuthenticationMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Session.SessionMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware.Invoke(HttpContext context)
Microsoft.AspNetCore.Diagnostics.DeveloperExceptionPageMiddleware.Invoke(HttpContext context)
A to moje dwie akcje. Chcę z ChangePassword do Index
ChangePassword
public async Task<ActionResult> ChangePassword([Bind(Prefix = "ChangePasswordViewModel")]ChangePasswordViewModel model)
{
// In case we have simple errors - return
if (!ModelState.IsValid)
{
TempData["ViewData"] = ViewData;
return RedirectToAction("Index");
}
var user = await _userManager.FindByIdAsync(this.User.FindFirstValue(ClaimTypes.NameIdentifier));
var result = await _userManager.ChangePasswordAsync(user, model.OldPassword, model.NewPassword);
if (result.Succeeded)
{
//var user1 = await _userManager.FindByIdAsync(this.User.FindFirstValue(ClaimTypes.NameIdentifier));
if (user != null)
{
await SignInAsync(user, isPersistent: false);
}
return RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess });
}
AddErrors(result);
// In case we have login errors
if (!ModelState.IsValid)
{
TempData["ViewData"] = ViewData;
return RedirectToAction("Index");
}
var message = ManageMessageId.ChangePasswordSuccess;
return RedirectToAction("Index", new { Message = message });
}
index
public async Task<ActionResult> Index(ManageMessageId? message)
{
if (TempData["ViewData"] != null)
{
ViewData = (ViewDataDictionary)TempData["ViewData"];
}
if (User.IsInRole("Admin"))
ViewBag.UserIsAdmin = true;
else
ViewBag.UserIsAdmin = false;
var user = await _userManager.FindByIdAsync(this.User.FindFirstValue(ClaimTypes.NameIdentifier));
if (user == null)
{
return View("Error");
}
var userLogins = await _userManager.GetLoginsAsync(user);
var otherLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
//var otherLoginsproviders = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
//var z = _db.UserData.Where(a => a.UserDataId == user.UserDataId).SingleOrDefault();
var model = new ManageCredentialsViewModel
{
Message = message,
HasPassword = this.HasPassword(),
CurrentLogins = userLogins,
OtherLogins = otherLogins,
ShowRemoveButton = user.PasswordHash != null || userLogins.Count > 1,
ChangeProfileViewModel = new ChangeProfileViewModel
{
FirstName = user.FirstName,
LastName = user.LastName,
Address = user.Address,
CodeAndCity = user.CodeAndCity,
PhoneNumber = user.PhoneNumber,
Email = user.Email
}
};
return View(model);
}
Błąd jest przy wyjściu z ChangePassword
w tym miejscu
if (!ModelState.IsValid)
{
TempData["ViewData"] = ViewData;
return RedirectToAction("Index");
}
Pozdrawiam