Extension methods cannot be dynamically dispatched error - how do I solve this?

asp.net-mvc-3, asp.net-mvc-4

Solution

You can call

@(InputExtensions.Hidden(Html, "myData", new MvcSerializer().Serialize(Model, SerializationMode.Signed)))

instead of `@Html.Hidden(...)` It is calling the extension method without the extension method syntax.

Problem

Can't find the proper solution to this problem. I am using `[Serializable]` (MVC3 Futures) in order to have a "wizard" with separate views. Here is the code in my controller to serialize: ``` private MyViewModel myData; protected override void OnActionExecuting(ActionExecutingContext filterContext) { var serialized = Request.Form["myData"]; if (serialized != null) //Form was posted containing serialized data { myData = (MyViewModel)new MvcSerializer().Deserialize(serialized, SerializationMode.Signed); TryUpdateModel(myData); } else myData = (MyViewModel)TempData["myData"] ?? new MyViewModel(); TempData.Keep(); } protected override void OnResultExecuted(ResultExecutedContext filterContext) { if (filterContext.Result is RedirectToRouteResult) TempData["myData"] = myData; } ``` Further along in my controller I do something like this (just a snippet - code goes through wizard with next and back button strings): ``` public ActionResult Confirm(string backButton, string nextButton) { if (backButton != null) return RedirectToAction("Details"); else if ((nextButton != null) && ModelState.IsValid) return RedirectToAction("Submitted"); else return View(myData); } ``` In my `.cshtml` view, I have this: ``` @using (Html.BeginFormAntiForgeryPost()) { @Html.Hidden("myData", new MvcSerializer().Serialize(Model, SerializationMode.Signed)) ... @Html.TextBoxFor(m => model.Step.EMail) ... } ``` Because I am using dynamics, I have to use a variable instead in the view: ``` var model = (MyViewModel) Model.myData; ``` in order to do the `@Html.TextBoxFor` above. And herein lies my probelm, because if I do `@model MyViewModel` instead, then I can't do `model.Step.EMail`. But because of dynamics, the `@Html.Hidden` won't work and I get the following error: Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'Hidden' but appears to have an extension method by that name. Extension methods cannot be dynamically dispatched. Consider casting the dynamic arguments or calling the extension method without the extension method syntax. I can switch to some other way of doing this without `[Serializable]`, but then I have to convert a LOT of code. Is there any way to make this work?

Original source