Pass the ViewBag as a Paramerter
Solution 1
As the error message tells you, extension methods cannot be dynamically dispatched. It's just not supported in .NET. It has nothing to do with ASP.NET MVC or Razor. Try writing an extension method to some type that takes a dynamic argument and then try invoking this extension method passing it a dynamic variable and you will get a compile time error.
Consider the following console application example which illustrates this:
public static class Extensions
{
public static void Foo(this object o, dynamic x)
{
}
}
class Program
{
static void Main()
{
dynamic x = "abc";
new object().Foo(x); // Compile time error here
}
}
So you need to cast:
@Html.SimpleHelper((string)ViewBag.SomeValue)
Actually, as Adam said you need to use strongly typed view model and never use ViewBag. It's just one of the millions of reasons why ViewBag shouldn't be used.
Solution 2
Even more importantly since ViewBag is somewhat of a bad practice to use because of magic strings as properties on viewbag - what are you trying to send to it. Maybe there is a better way? You should be able to use the helper instance to reference it via:
helper.ViewContext.Controller.ViewBag
but I'm not one for using ViewBag except only for Title http://completedevelopment.blogspot.com/2011/12/stop-using-viewbag-in-most-places.html
Comments
-
TYRONEMICHAEL almost 2 years
If I have a value inside the Dynamic
ViewBag
, why can't I pass it to a method? So for simplicity's sake, lets say I haveViewBag.SomeValue
, and I want to pass theViewBag.SomeValue
to an HTML Helper. If the HTML helper accepts dynamic as a variable, why wont it accept myViewBag.SomeValue
?@Html.SimpleHelper(ViewBag.SomeValue) public static string SimpleHelper(this HtmlHelper html, dynamic dynamicString) { return string.Format("This is my dynamic string: {0}", dynamicString); }
-
TYRONEMICHAEL over 12 yearsI prefer to use strongly typed models and pass the model into the helper. But lets say for example you wanted to pass the title, in this case a ViewBag.SomeValue, into an html helper. Now you could declare a string and make that equal to the Viewbag and then pass the string into the helper. But that just makes the markup that much more messy. I have edited my code above to show you more of what I am trying to achieve.
-
Adam Tuliper over 12 yearsI was pretty sure my answer above to provide a fix in your specific case if you so choose to do it that way.
-
Adam Tuliper over 12 yearsYou can still ref it via helper.ViewContext.Controller.ViewBag Though no?
-
Matt over 11 years+1 - after reading this I realised I was about to do something stupid. You saved me :)
-
Kushal Shah almost 11 yearsNo, access via return (string) helper.ViewData["Title"];
-
Flood Gravemind over 10 years@AdamTuliper-MSFT I am not sure if it is a bad practise necessarily. Eg if you have a large querystring values to pass back from controller to view for display purposes then it is fine because that info is already available to user?
-
Adam Tuliper over 10 yearsI prefer to say never use it. Why not in this case just add something to a view model? Then you always know all of the data that goes to the view. Any ViewData is a brittle implementation, is not good for refactoring, etc.