Mvc UserControl using HtmlHelper with HelperResult and MvcHtmlString

人走茶凉 提交于 2019-12-01 18:14:29

问题


I'm developing an application in asp mvc, and I want to create some friendly controls for developers, like Server controls in asp.net WebForms . In this case, I want to create a bootstrap panel that has some fixed html and render inside a template of items. I am doing it this way:

public static MvcHtmlString BootPanel<T>(this IEnumerable<T> items, Func<T, HelperResult> template, string title = "")
    {

     StringBuilder sb = new StringBuilder();
     sb.Append("<div class='panel panel-info'>");
     sb.Append("<div class='panel-heading'>");
     sb.Append(title);
     sb.Append("</div>");
     sb.Append("<div class='panel-body'>");

     var hr = new HelperResult(writer =>
              {
                 foreach (var item in items)
                {
                  template(item).WriteTo(writer);
                }
       });

      sb.Append(hr.ToHtmlString());
      sb.Append("</div>");
      sb.Append("</div>");
      return MvcHtmlString.Create(sb.ToString());
    }

This is the razor View:

@{
  var videoGames = new[] {
      new VideoGame {nombre = "Shadows of mordor", precio = 66},
      new VideoGame {nombre = "Spiderman", precio = 50}
      };
   }    

  @videoGames.BootPanel(@<div>
                            @item.nombre  &nbsp;  @item.precio
                        </div>
                      , "Titulo")

The output is rendered correctly using the bootstrap classes:

The question is. Is this a correct way of implementing this kind of control?

EDIT:

Following your answers I updated the helper yo use TagBuilder:

public static MvcHtmlString BootPanel<T>(this IEnumerable<T> items, Func<T, HelperResult> template, string title = "")
    {
        TagBuilder PanelDiv = new TagBuilder("div");
        PanelDiv.AddCssClass("panel panel-info");

        TagBuilder PanelHeading = new TagBuilder("div");
        PanelHeading.AddCssClass("panel-heading");
        PanelHeading.SetInnerText(title);

        TagBuilder PanelBody = new TagBuilder("div");
        PanelBody.AddCssClass("panel-body");

        var hr = new HelperResult(writer =>
        {


            foreach (var item in items)
            {
                template(item).WriteTo(writer);
            }

        });

        PanelBody.InnerHtml = hr.ToHtmlString();

        PanelDiv.InnerHtml = PanelHeading.ToString() +
                             PanelBody.ToString();

        return MvcHtmlString.Create(PanelDiv.ToString());
    }

回答1:


Make sure you encode the output string before returning since if some one writes a javascript for title then it results in script injection. Apart from that it looks, but instead using hard coded tags you may use TagBuilder class to build your html code. See this example.



来源:https://stackoverflow.com/questions/26362910/mvc-usercontrol-using-htmlhelper-with-helperresult-and-mvchtmlstring

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