TYRONEMICHAEL
TYRONEMICHAEL

Reputation: 4244

Pass the ViewBag as a Paramerter

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);
}

Upvotes: 6

Views: 11460

Answers (4)

DavSin
DavSin

Reputation: 124

I created one extension method.

public static IHtmlContent BindDropdown(this IHtmlHelper htmlHelper, SelectList viewBag)
    {
        StringBuilder sb = new StringBuilder();
        foreach (var item in viewBag)
        {
            sb.Append("<option value=\"" + item.Value + "\">" + item.Text + " </option>"); 
        }
        return new HtmlString(sb.ToString());
    }

And like below used in the Razor Page.

<select>@Html.BindDropdown((SelectList)ViewBag.Currency) </select>

On the controller side, my dropdown ViewBag was

ViewBag.Currency = new SelectList(_currencyService.GetAll().Select(i => new { i.Id, Name = i.Code }).ToArray(), "Id", "Name");

Upvotes: 0

Christopher Johnson
Christopher Johnson

Reputation: 665

I know this is an older question but it's what came up for me when I searched for this issue.

It's easy enough to pass the ViewBag as a parameter by simply listing it as a dynamic in the method. For example here is how I pass the ViewBag into one of my helper classes for column sorting.

public static IEnumerable<Models.MyViewModel> MyMethod(IEnumerable<Models.MyViewModel> model, string sortOrder, dynamic ViewBag)

Upvotes: 0

Darin Dimitrov
Darin Dimitrov

Reputation: 1038720

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.

Upvotes: 8

Adam Tuliper
Adam Tuliper

Reputation: 30152

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

Upvotes: 3

Related Questions