Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • MK 429 posts 906 karma points
    Dec 23, 2012 @ 08:47
    MK
    0

    Razor render White-space after html element

    Hi there,

    I have this simple form (via razor in contour 3):

       <li>
                                        <div class="name">@f.Caption</div>
                                        <div class="info">@Html.Partial(FieldViewResolver.GetFieldView(Model.FormId, f.FieldTypeName, f.Caption), f)</div>
                                </li>    

    and for some reason I get \r\n in the html just after the <div class="info">.

    Any idea on how to get rid of it?

    Thanks

    mkariti

  • Jan Skovgaard 11280 posts 23678 karma points MVP 11x admin c-trib
    Dec 23, 2012 @ 11:22
    Jan Skovgaard
    0

    Hi mkariti

    Is it from this line you get the \r\n? @Html.Partial(FieldViewResolver.GetFieldView(Model.FormId, f.FieldTypeName, f.Caption), f) - if so try adding Replace in the end like @Html.Partial(FieldViewResolver.GetFieldView(Model.FormId, f.FieldTypeName, f.Caption), f).Replace("\r\n","");

    Hope this helps.

    /Jan

  • MK 429 posts 906 karma points
    Dec 23, 2012 @ 11:53
    MK
    0

    Hi Jan,

    Thanks but the problem persist.

    I can seems to refference the System.Web.Mvc.dll but not use it from the razor file?!

    Also, I get the following error:

    System.Web.Mvc.MvcHtmlString' does not contain a definition for 'Replace'

    Tried to string it and htmled it again but still get the \r\n

    Any other idea?

    Cheers

    mkariti

  • MK 429 posts 906 karma points
    Dec 24, 2012 @ 11:34
    MK
    0

    Any help with this one will much appriciated.

  • Grant Thomas 291 posts 324 karma points
    Dec 24, 2012 @ 12:16
    Grant Thomas
    0

    Generally, in the most simple fashion, linebreaking sequences are replaced with equivalent HTML linebreak elements (as opposed to no space at all), so, say, a <br/> tag.

    MvcHtmlString obviously doesn't contain all methods of string, yet is a glorified representation of a string, so you can get the literal by calling .ToString() to resolve the error, then do a replace on the result, such that:

    @Html.Partial(FieldViewResolver.GetFieldView(Model.FormId, f.FieldTypeName, f.Caption), f).ToString().Replace("\r\n","<br/>");
  • MK 429 posts 906 karma points
    Dec 24, 2012 @ 12:27
    MK
    0

    Hi Thomas,

    Thanks for your reply.

    The problem is that now your solution will be rendered as string rather than HTML. 

    If you will try to convert it back to HTML the whitespace/br will be inserted again.

    I managed to do the trick with jquery but I would prefer to nail it down in the rezor.

    Cheers

    mkariti

     

  • Grant Thomas 291 posts 324 karma points
    Dec 24, 2012 @ 12:54
    Grant Thomas
    0

    Use @Html.Raw to output the resulting HTML literal.

  • MK 429 posts 906 karma points
    Dec 24, 2012 @ 13:15
    MK
    0

    Thanks Thomas,

    But still same prob.


  • Comment author was deleted

    Dec 27, 2012 @ 10:40

    Unfortunalty the whitespace is due to a bug in the razor view engine http://forums.asp.net/p/1618460/4149182.aspx if you restructure the view you can get rid of it

  • MK 429 posts 906 karma points
    Dec 27, 2012 @ 10:45
    MK
    0

    Thanks Tim

  • joyefox 25 posts 95 karma points
    Feb 22, 2013 @ 12:36
    joyefox
    0

    Hi mkariti,

    I've a similar problem. How did you exactly solve it? Changing the way you call the partial view render?

    Regards

    Matteo

  • MK 429 posts 906 karma points
    Feb 22, 2013 @ 13:18
    MK
    0

    Hi joyefox,

    In my case I had to find a quick and easy solution so I  just used jquery to remove it:

     $('.info').each(function (index) {

            $(this).html($.trim($(this).html()));

        });

    Not elegant as reconstructing the view but did the trick.

    Regards,

    mkariti

  • Mads Krohn 211 posts 504 karma points c-trib
    Apr 08, 2013 @ 01:24
    Mads Krohn
    0

    Tried the javascript approach, which seems hacky at best, but that requires you to wrap the input field in a container, like a div, which seems to break the conditional-field-logic-stuff, at least in my code.

    So, does anybody have an idea on how to restructure the view to get rid of the whitespaces?

  • Comment author was deleted

    Apr 22, 2013 @ 15:33

    Pushed a workaround in 3.0.10 WIP that should fix the whitespace issues

  • Anders Brohäll 295 posts 561 karma points c-trib
    Nov 22, 2013 @ 17:59
    Anders Brohäll
    1

    The way i found easiest is to open the files in Notepad++, change the encoding to UTF-8, without BOM and save them.

    100% of allt the times, it works every time.

Please Sign in or register to post replies

Write your reply to:

Draft