开发者

Razor syntax to declare action that renders html

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.


@{
    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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜