Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extension methods cannot be dynamically dispatched in Textbox

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 ?

like image 930
TechGuy Avatar asked Dec 11 '22 03:12

TechGuy


1 Answers

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.

like image 86
Kartikeya Khosla Avatar answered Jan 18 '23 23:01

Kartikeya Khosla