Contents tagged with TagBuilder

  • Trivial implementation of File uploader

    Hello everybody,

    today I want to share with you fragment of code of html extension.

    Imagine that you edit your cshtml file, and see following code:

    @Html.EditorFor(model => model.SomeField

    Then you need to add some file upload and notice, that there is not file upload! 

    How can you fix this situation?

    I propose to use HtmlExtension code. For example like this:

    public static MvcHtmlString FileUpload(this HtmlHelper htmlHelper, string cssClass, object htmlAttrs = null) {

    var tb = new TagBuilder("input");

    tb.MergeAttribute("type", "file");

    tb.MergeAttributes(HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttrs));

    if (!string.IsNullOrWhiteSpace(cssClass)) {

    tb. … more