I have a method in my basecontroller class that adds data to tempdata to display pop-up messages.
protected void AddPopupMessage(SeverityLevels severityLevel, string title, string message)
{
var newPopupMessage = new PopupMessage()
{
SeverityLevel = severityLevel,
Title = title,
Message = message
};
_popupMessages.Add(newPopupMessage);
TempData["PopupMessages"] = _popupMessages;
}
If the action returns a view, this works fine. If the action is calling a redirectotoaction I get the following error.
InvalidOperationException: The 'Microsoft.AspNetCore.Mvc.ViewFeatures.Internal.TempDataSerializer' cannot serialize an object of type
Any thoughts ?
TempData in MVC is used to pass data from Controller to Controller or Controller to view and it is used to pass data that persists only from one request to the next. TempData requires Typecasting. And also check for Null value before reading the value from it.
TempData is used to pass data from current request to subsequent request (i.e., redirecting from one page to another). Its life is too short and lies only till the target view is fully loaded. But you can persist data in TempData by calling the method Keep () .
Passing the data from Controller to View using TempDataGo to File then New and select “Project” option. Then create the ASP.NET web application project as depicted below. Then select “Empty” and tick “MVC” then click OK. The project is created successfully.
TempData is used to transfer data from view to controller, controller to view, or from one action method to another action method of the same or a different controller. TempData stores the data temporarily and automatically removes it after retrieving a value. TempData is a property in the ControllerBase class.
My issue was that, after filling in some TempData values and calling RedirectToAction (), TempData would be empty on the page that I was redirecting to. Per HamedH's answer here : If you are running ASP.NET Core 2.1, open your Startup.cs file and make sure that in your Configure () method app.UseCookiePolicy (); comes after app.UseMVC ();.
You can not redirect to a post. A redirect is alway a get. Also it looks like you using asp.net core, and the default implementation of temp data is to store it in a cookie. So you should be sure the data is small. As a cookie value is a string, this why tempdata is a string.
[Microsoft.AspNetCore.Mvc.NonAction] public virtual Microsoft.AspNetCore.Mvc.RedirectToActionResult RedirectToAction (); The created RedirectToActionResult for the response. A POST request to an action named "Product" updates a product and redirects to an action, also named "Product", showing details of the updated product.
By default, data should be stored in cookies only with the user's consent. If the user does not agree with the storing data in cookies, TempData cannot work. This behavior varies across versions of ASP NET Core. If you do not want to hold any sensitive data in cookies, you can obviously change the settings.
TempData
uses Session
, which itself uses IDistributedCache
. IDistributedCache
doesn't have the capability to accept objects or to serialize objects. As a result, you need to do this yourself, i.e.:
TempData["PopupMessages"] = JsonConvert.SerializeObject(_popupMessages);
Then, of course, after redirecting, you'll need to deserialize it back into the object you need:
ViewData["PopupMessages"] = JsonConvert.DeserializeObject<List<PopupMessage>>(TempData["PopupMessages"]);
Another scenario where this type of error happened to me was upgrading from AspNetCore 2.2 to 3.0 - and, where I had AzureAD as an OpenID Connect provider - when it would try to process the AzureAD cookie, it threw an exception the TempDataProvider and complained that it could not serialize a type of Int64. This fixed:
services.AddMvc()
.AddNewtonsoftJson(options =>
options.SerializerSettings.ContractResolver =
new CamelCasePropertyNamesContractResolver());
https://learn.microsoft.com/en-us/aspnet/core/migration/22-to-30?view=aspnetcore-3.1&tabs=visual-studio#newtonsoftjson-jsonnet-support
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With