C# – Extension methods cannot be dynamically dispatched in Textbox

asp.net-mvcasp.net-mvc-5centity-framework

Herei'm going to take a values from ViewBag.But below error occured.

Specific Error Message is

Compiler Error Message: CS1973: 'System.Web.Mvc.HtmlHelper' has no applicable method named 'TextBox' 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.

ViewBag in Controller

 ViewBag.Vat = tx.GetVatDetails().FirstOrDefault().Percentage; // result is 15.0 

In my View

 @Html.TextBox("vat",ViewBag.Vat); // in here that error occured

How do i solve this ?

Best Answer

This will work :-

@Html.TextBox("vat",(float)ViewBag.Vat);  //Type cast `ViewBag` to float here

Casting of the Viewbag data to float worked because Extension methods cannot be dynamically dispatched and Viewbag is a dynamic object that is why we have to first typecast it into appropriate type which is float here.