Pass the ViewBag as a Paramerter

10,495

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

Share:
10,495
TYRONEMICHAEL
Author by

TYRONEMICHAEL

Senior Software Engineer

Updated on June 09, 2022

Comments

  • TYRONEMICHAEL
    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 have ViewBag.SomeValue, and I want to pass the ViewBag.SomeValue to an HTML Helper. If the HTML helper accepts dynamic as a variable, why wont it accept my ViewBag.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
    TYRONEMICHAEL over 12 years
    I 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
    Adam Tuliper over 12 years
    I 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
    Adam Tuliper over 12 years
    You can still ref it via helper.ViewContext.Controller.ViewBag Though no?
  • Matt
    Matt over 11 years
    +1 - after reading this I realised I was about to do something stupid. You saved me :)
  • Kushal Shah
    Kushal Shah almost 11 years
    No, access via return (string) helper.ViewData["Title"];
  • Flood Gravemind
    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
    Adam Tuliper over 10 years
    I 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.