Copied to clipboard

Flag this post as spam?

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


  • Richard Boelen 61 posts 153 karma points
    Apr 20, 2011 @ 21:07
    Richard Boelen
    0

    Razor macro scripts producing whitespaces and linebreaks

    Hi There,

    I'm using a razor macro for some simple form stuff. In this macro I use inline @helper methods for rendering out parts of the html.

    This works fine, however produces lots of whitespaces and linebreaks.

     

    This is the .cshtml file for the macro:

    @helper MessageForm(){
      <form method="post"><input type="submit" name="@Parameter.SubmitName" value="submit"/></form>  
    }
    
    @helper MessageHandler(){
        <h1>Hello from @Parameter.SubmitName</h1>
    }
    
    @{var request = HttpContext.Current.Request;
      var isPost = request.HttpMethod == "POST";
      string submitName = Parameter.SubmitName;
      var shouldHandle = request.Form.AllKeys.Contains(submitName);
    
      if (isPost && shouldHandle) {
        @MessageHandler()
      }
      @MessageForm()
    }

    I use 2 of these macro's on a page, giving me to forms each with a submit button.

    This is the resul in html after pressing the second button. It gives me at least 4 linebreaks before rendering each  macro (not sure if it's visible in the code view here).

     
     
     
     
     
      <form method="post"><input type="submit" name="ButtonA" value="submit"/></form>
     
     
     
     
      <h1>Hello from ButtonB</h1>
      <form method="post"><input type="submit" name="ButtonB" value="submit"/></form>
      

    Seems that rendering the .cshtml does not remove the linbreaks and whitespace before and after the @{ ... } code blocks.

    Is this a Razor thing or is it the way Umbraco is hosting the Razor viewengine?

    Anybody?

    Thanks, Richard

     

  • Sebastiaan Janssen 5045 posts 15477 karma points MVP admin hq
    Apr 21, 2011 @ 15:45
    Sebastiaan Janssen
    0

    Hmm, I haven't seen this before, it's not a huge problem is it? You could try restructuring a little:

    @{
      var request = HttpContext.Current.Request;
      var isPost = request.HttpMethod == "POST";
      string submitName = Parameter.SubmitName;
      var shouldHandle = request.Form.AllKeys.Contains(submitName);
    }
    @helper MessageForm(){
      <form method="post"><input type="submit" name="@Parameter.SubmitName" value="submit"/></form>  
    }
    @helper MessageHandler(){
        <h1>Hello from @Parameter.SubmitName</h1>
    }
    @if (isPost && shouldHandle) {
        @MessageHandler()
    }
    @MessageForm()
  • Jonas Eriksson 930 posts 1825 karma points
    May 01, 2011 @ 09:01
    Jonas Eriksson
    0

    I agree it is with those whitespaces. Seems that this bug is not fixed in the Razor engine (the MS one), http://forums.asp.net/p/1618460/4149182.aspx

    I intended to write information like

    John Doe, e-mail: [email protected], phone: 000-000 00 00

    but needed to add a condition to omit the e-mail and phone address if there was none.

    I first got this result

    John Doe , e-mail: [email protected] , phone: 000-000 00 00

    To get rid an extra whitespace before the comma's I came up with this ugly code:

    <strong>@dn.name</strong>@if (dn.email!=""){@:, e-mail: <a href="mailto:@dn.email">@dn.email</a>
    }@if (dn.phone!=""){@:, phone: @dn.phone
    }

    (Hm, I'm using IE9, and cannot get code formatting to work)

    Regards

    Jonas

Please Sign in or register to post replies

Write your reply to:

Draft