As you know, we can create two types of Helper methods in ASP.NET MVC:
- Inline helper methods
- External helper methods
And now, let's say I am passsing data from the controller to the view by using the ViewBag
object. Like that:
ViewBag.Fruits = new string[] {"Apple", "Orange", "Pear"};
And I have defined such inline helper method in the view:
@helper ListArrayItemsInline(string[] items)
{
...
}
And here is an external helper method which takes a string array as an input:
public static MvcHtmlString ListArrayItemsExternal(this HtmlHelper html, string[] list)
{
...
}
And the difference is that, I must cast ViewBag.Fruits
to string[]
if I want to use external one. Yes, everything is right here. But, this is not the case with the inline one. As I see, it evaluates types at runtime.
// external one, we must cast
@Html.ListArrayItemsExternal((string[])ViewBag.Fruits)
// internal one, works just fine
@ListArrayItemsInline(ViewBag.Fruits)
Could you please explain me, how and why inline helper methods evaluating types at runtime?