0
votes

I'm trying to create an action that renders some html in the razor view engine. This was pretty easy in the webforms engine but I'm having some issues with razor. Here is the simplest form of what I'm trying to accomplish using the webforms engine:

<% var myAction = new Action<HtmlHelper<int>>((helper) => { %>
    <div>
        <%= helper.ViewData.Model %>
    </div>
<%}); %>

The closest I've come in the razor view engine is:

@{var myAction = new Action<HtmlHelper<int>>((help) =>
              {
                    @<div>
                        @help.ViewData.Model
                    </div>;
              });
}

This gives a "CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement" error.

Any help would be appreciated. Thanks.

1

1 Answers

1
votes
@{
    Func<dynamic, object> myAction = 
        @<div>
            @item.ProductName
        </div>;
}

@myAction(Model)

You may also checkout the following blog post.


UPDATE:

You may also do this:

@{
    Func<HtmlHelper<int>, object> myAction = @<div>@item.ViewData.Model</div>;
}

or:

@{
    Func<dynamic, object> myAction = @<div>@item.ViewData.Model</div>;
}

and to invoke:

@myAction(someInstanceOfTheRequiredHelper)