Helper methods to generate small HTML snippets

二次信任 提交于 2019-12-06 14:21:28
filhit

I believe there are four common solutions to reuse html as you want in the question.

  1. Partial views (@Html.Partial);
  2. Child Actions (@Html.Action);
  3. Custom static helpers (@Html.Whatever, @Url.Whatever, extension methods to your models, etc);
  4. Razor helpers. (@helper)

Is it OK to use static helper classes to generate small HTML snippets?

Is it absolutely OK to use static helper classes to generate small HTML snippets. I don't like the idea to add them as methods to the models, extension methods are OK.

I would say the difference between the approaches lies mostly in coding style and personal preferences. I would use partial views just to break a large view into consumable pieces, child actions for really common and independent parts of the application (like a widget or a login box), so I don't have to populate all my view models with the data for common things. I would go static helpers or razor helpers for very small html fragments (a field in a form) with static helpers for more code and razor helpers for more html.

Where should the static UserInfo class go? Views? Controllers? Elsewhere?

These things belong to View from the pattern perspective. If you ask which folders should you populate in the solution, I would recommend a special folder for them (maybe HtmlHelpers). There is probably a restriction for shared razor helpers to reside in App_Code folder.

I think the following questions will give you more on how to choose between them:

  1. Creating reusable HTML view components using Razor in ASP.NET MVC;
  2. How to create reusable control in ASP.NET MVC;
  3. How to create a reusable piece of HTML with dynamic contents;
  4. ASP.NET MVC: Razor @helper vs extension methods to HtmlHelper - which is preferred?.

UPDATE

There may be a fifth solution in the new ASP.NET MVC: View Components. As far as I read you can use them instead of child actions.

It's hard to say what your best path is based on what you've provided here. A lot of it depends on what you're working with and what you're trying to achieve.

For example, the user info stuff you're currently including via partial views, sounds like it would be better served by a child action:

[Authorize]
public class AccountController : Controller
{
    ...

    [ChildActionOnly]
    [AllowAnonymous]
    public ActionResult UserInfo()
    {
        // get your user info here
        return PartialView("UserInfo", userInfo);
    }
}

And then in your view/layout:

@Html.Action("UserInfo", "Account")

Then, you don't need to make sure that there's a user object populated on whatever view model you're working with.

Next, the Razor helper methods like Html.Partial, etc. are themselves simply extensions to either HtmlHelper or UrlHelper for the Url.* helpers. There's nothing wrong with adding your own extension methods. It was quite common to extend HtmlHelper with a custom EnumDropDownListFor, for example, before it got baked into MVC 5 out of the box. However, extensions are really best suited to small bits of HTML, which seems to be what you want to work with here. For large blocks of HTML it just makes better sense to use partials. Either works, though. It's largely up to you to just decide what makes the most sense for your application.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!