Cannot find Html.Serialize helper in MVC 5 futures

2019-01-19 07:58发布

问题:

I just installed MVC 5 futures in my solution using Package Manager, but I cannot find this helper method Html.Serialize, which was there in previous MVC Futures releases.

My Question: What namespace I need to include to start using Html.Serialize helper method with MVC 5 Futures?

回答1:

Apparently, this extension helper is no longer included in the current MVC Futures.

In my case, I replaced the function call Html.Serialize by MvcSerializer.Serialize method which is included in Microsoft.Web.Mvc namespace.

To serialize any object in a hidden field:

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

Later, the controller can turn back the initial object:

[HttpPost]
public ActionResult Index(
    IndexViewModel model,
    [Deserialize] DataType otherComplexData
)

I hope you find it useful.