Danny Cullen February 2016

How to serialize an object in MVC 5 Razor view

I have tried to follow this post in order to create a 3 page form wizard that passes data to each page.

He uses the HTML helper serialize, to serialize an object in the view.

@Html.Serialize("wizard", Model)

However this HTML helper isn't available in MVC 5 it seems.

I found another related post to this here where he suggests using the following to serialize the object.

@Html.Hidden("otherComplexData", new Microsoft.Web.Mvc.MvcSerializer().Serialize(complexObject))

But I then get the following error

There is no argument given that corresponds to the required formal parameter 'mode' of 'MvcSerializer.Serialize(object, SerializationMode)'

It seems to want a SerializationMode, however the documented one doesn't. https://msdn.microsoft.com/en-us/library/microsoft.web.mvc.mvcserializer.serialize(v=vs.118).aspx

What direction can I go in now?

Thanks.

Answers


bradlis7 February 2016

Here's the Serialization option you need:

https://github.com/ASP-NET-MVC/ASP.NET-Mvc-3/blob/master/mvc3/src/MvcFutures/Mvc/SerializationMode.cs

Options are Signed or EncryptedAndSigned.

You can try that and see if it will work.

There's multiple ways to encode data that will work for you. You could put the values in a hidden input using Json.Encode for the view, and Json.Decode on the server side.

Post Status

Asked in February 2016
Viewed 1,789 times
Voted 11
Answered 1 times

Search




Leave an answer