Why Is My Razor Code Not Working With Tag Helpers in ASP.NET Core





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1















My question up front is why will razor syntax not work inside of a tag where I am using tag helpers? It is something I can work around and a solution to the problem has been answered HERE, but I still have not been able to find out why this problem occurs.





As an example of this I have a select tag that I am trying to add a disabled attribute to only if the model has a certain property. Here is the html:



<select 
@(Model.Id != 0 ? "disabled" : "")
asp-for="QuestionType"
asp-items="Html.GetEnumSelectList<Enums.QuestionTypes>()"
id="form-type" data-parsley-required class="form-control">
</select>


Using Razor inside of the select outputs the following



<select 
asp-for="QuestionType"
asp-items="Html.GetEnumSelectList<Enums.QuestionTypes>()"
id="form-type" data-parsley-required="" class="form-control">
</select>


Not using Razor gives this



<select 
id="form-type" data-parsley-required="" class="form-control"
data-val="true" data-val-required="The QuestionType field is required."
name="QuestionType">
<option selected="selected" value="0">Drop Down</option>
<option value="10">Free Response</option>
</select>




While using Razor syntax, it seems to ignore the tag helpers and does not build the HTML as it should (or as is expected). However, using razor inside of attribute values seems to work fine, it's when I use it to try and add things like tag attributes that I get these issues. As stated before, I am just trying to understand why this is happening.










share|improve this question































    1















    My question up front is why will razor syntax not work inside of a tag where I am using tag helpers? It is something I can work around and a solution to the problem has been answered HERE, but I still have not been able to find out why this problem occurs.





    As an example of this I have a select tag that I am trying to add a disabled attribute to only if the model has a certain property. Here is the html:



    <select 
    @(Model.Id != 0 ? "disabled" : "")
    asp-for="QuestionType"
    asp-items="Html.GetEnumSelectList<Enums.QuestionTypes>()"
    id="form-type" data-parsley-required class="form-control">
    </select>


    Using Razor inside of the select outputs the following



    <select 
    asp-for="QuestionType"
    asp-items="Html.GetEnumSelectList<Enums.QuestionTypes>()"
    id="form-type" data-parsley-required="" class="form-control">
    </select>


    Not using Razor gives this



    <select 
    id="form-type" data-parsley-required="" class="form-control"
    data-val="true" data-val-required="The QuestionType field is required."
    name="QuestionType">
    <option selected="selected" value="0">Drop Down</option>
    <option value="10">Free Response</option>
    </select>




    While using Razor syntax, it seems to ignore the tag helpers and does not build the HTML as it should (or as is expected). However, using razor inside of attribute values seems to work fine, it's when I use it to try and add things like tag attributes that I get these issues. As stated before, I am just trying to understand why this is happening.










    share|improve this question



























      1












      1








      1








      My question up front is why will razor syntax not work inside of a tag where I am using tag helpers? It is something I can work around and a solution to the problem has been answered HERE, but I still have not been able to find out why this problem occurs.





      As an example of this I have a select tag that I am trying to add a disabled attribute to only if the model has a certain property. Here is the html:



      <select 
      @(Model.Id != 0 ? "disabled" : "")
      asp-for="QuestionType"
      asp-items="Html.GetEnumSelectList<Enums.QuestionTypes>()"
      id="form-type" data-parsley-required class="form-control">
      </select>


      Using Razor inside of the select outputs the following



      <select 
      asp-for="QuestionType"
      asp-items="Html.GetEnumSelectList<Enums.QuestionTypes>()"
      id="form-type" data-parsley-required="" class="form-control">
      </select>


      Not using Razor gives this



      <select 
      id="form-type" data-parsley-required="" class="form-control"
      data-val="true" data-val-required="The QuestionType field is required."
      name="QuestionType">
      <option selected="selected" value="0">Drop Down</option>
      <option value="10">Free Response</option>
      </select>




      While using Razor syntax, it seems to ignore the tag helpers and does not build the HTML as it should (or as is expected). However, using razor inside of attribute values seems to work fine, it's when I use it to try and add things like tag attributes that I get these issues. As stated before, I am just trying to understand why this is happening.










      share|improve this question
















      My question up front is why will razor syntax not work inside of a tag where I am using tag helpers? It is something I can work around and a solution to the problem has been answered HERE, but I still have not been able to find out why this problem occurs.





      As an example of this I have a select tag that I am trying to add a disabled attribute to only if the model has a certain property. Here is the html:



      <select 
      @(Model.Id != 0 ? "disabled" : "")
      asp-for="QuestionType"
      asp-items="Html.GetEnumSelectList<Enums.QuestionTypes>()"
      id="form-type" data-parsley-required class="form-control">
      </select>


      Using Razor inside of the select outputs the following



      <select 
      asp-for="QuestionType"
      asp-items="Html.GetEnumSelectList<Enums.QuestionTypes>()"
      id="form-type" data-parsley-required="" class="form-control">
      </select>


      Not using Razor gives this



      <select 
      id="form-type" data-parsley-required="" class="form-control"
      data-val="true" data-val-required="The QuestionType field is required."
      name="QuestionType">
      <option selected="selected" value="0">Drop Down</option>
      <option value="10">Free Response</option>
      </select>




      While using Razor syntax, it seems to ignore the tag helpers and does not build the HTML as it should (or as is expected). However, using razor inside of attribute values seems to work fine, it's when I use it to try and add things like tag attributes that I get these issues. As stated before, I am just trying to understand why this is happening.







      razor asp.net-core asp.net-core-mvc tag-helpers asp.net-core-tag-helpers






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 3 at 17:05









      R. Richards

      14.9k94146




      14.9k94146










      asked Jan 3 at 16:43









      Jordan Lee BurnesJordan Lee Burnes

      758




      758
























          1 Answer
          1






          active

          oldest

          votes


















          1














          You cannot use a Razor expression inside a tag itself, only within the quotes of an attribute. I'm actually surprised it didn't raise an exception, but it seems it simply opted to treat the tag as plaintext instead. Regardless, you need to do something like the following instead:



          <select 
          disabled="@(Model.Id != 0 ? "disabled" : null)"
          asp-for="QuestionType"
          asp-items="@Html.GetEnumSelectList<Enums.QuestionTypes>()"
          id="form-type" data-parsley-required class="form-control">
          </select>


          If the value of an attribute is null, Razor will remove it, so you'll end up with disabled="disabled" if Model.Id is not equal to 0, and no disabled attribute at all if it is. You don't need to do anything crazy like the answers in the linked question suggest.



          EDIT



          Thinking about it more, I think it only went through because you broke the tag into multiple lines, so basically Razor treated the surrounding tag as invalid HTML and honed in on the line it could parse (the ternary expression). I'd imagine if you put the tag in a single line, you would in fact get an exception. Either way, it's a syntax error.






          share|improve this answer
























          • Sorry, I only broke it up into multiple lines for readability for the post. In my actual HTML it is all in one line. I checked my Chrome debugger as well as my logs (using boilerplate framework) and unfortunately no errors were thrown. Also, using your example, the html still is not built from the tag helpers and I get the same result as above where the compiled html has the tag helpers as attributes. I think I need to look at how the html is parsed in the Tag Helper class to see what's really going on.

            – Jordan Lee Burnes
            Jan 3 at 19:13






          • 1





            I can confirm that the error is not because it was written in multiple lines. I was able to perfectly reproduce this problem with a single-line tag-helper such as input.

            – ShadowKras
            Jan 3 at 19:22











          • Actually @ChrisPratt, your solution worked perfectly, I just got the syntax mixed up when testing it so thanks for that! It is much quicker than writing my own tag helper for sure!

            – Jordan Lee Burnes
            Jan 3 at 19:22








          • 1





            After some testing, earlier today, I managed to find out that the tag helper isnt even fired (as in, the Process method is never called, nor any of the tag helper properties ever defined) when reproducing the issue. So, we need to know why that happens, as there are no clues in the tag helper code.

            – ShadowKras
            Jan 3 at 19:27











          • @Chris Pratt This is a fine answer to the other question.

            – ShadowKras
            Jan 3 at 19:34












          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026425%2fwhy-is-my-razor-code-not-working-with-tag-helpers-in-asp-net-core%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You cannot use a Razor expression inside a tag itself, only within the quotes of an attribute. I'm actually surprised it didn't raise an exception, but it seems it simply opted to treat the tag as plaintext instead. Regardless, you need to do something like the following instead:



          <select 
          disabled="@(Model.Id != 0 ? "disabled" : null)"
          asp-for="QuestionType"
          asp-items="@Html.GetEnumSelectList<Enums.QuestionTypes>()"
          id="form-type" data-parsley-required class="form-control">
          </select>


          If the value of an attribute is null, Razor will remove it, so you'll end up with disabled="disabled" if Model.Id is not equal to 0, and no disabled attribute at all if it is. You don't need to do anything crazy like the answers in the linked question suggest.



          EDIT



          Thinking about it more, I think it only went through because you broke the tag into multiple lines, so basically Razor treated the surrounding tag as invalid HTML and honed in on the line it could parse (the ternary expression). I'd imagine if you put the tag in a single line, you would in fact get an exception. Either way, it's a syntax error.






          share|improve this answer
























          • Sorry, I only broke it up into multiple lines for readability for the post. In my actual HTML it is all in one line. I checked my Chrome debugger as well as my logs (using boilerplate framework) and unfortunately no errors were thrown. Also, using your example, the html still is not built from the tag helpers and I get the same result as above where the compiled html has the tag helpers as attributes. I think I need to look at how the html is parsed in the Tag Helper class to see what's really going on.

            – Jordan Lee Burnes
            Jan 3 at 19:13






          • 1





            I can confirm that the error is not because it was written in multiple lines. I was able to perfectly reproduce this problem with a single-line tag-helper such as input.

            – ShadowKras
            Jan 3 at 19:22











          • Actually @ChrisPratt, your solution worked perfectly, I just got the syntax mixed up when testing it so thanks for that! It is much quicker than writing my own tag helper for sure!

            – Jordan Lee Burnes
            Jan 3 at 19:22








          • 1





            After some testing, earlier today, I managed to find out that the tag helper isnt even fired (as in, the Process method is never called, nor any of the tag helper properties ever defined) when reproducing the issue. So, we need to know why that happens, as there are no clues in the tag helper code.

            – ShadowKras
            Jan 3 at 19:27











          • @Chris Pratt This is a fine answer to the other question.

            – ShadowKras
            Jan 3 at 19:34
















          1














          You cannot use a Razor expression inside a tag itself, only within the quotes of an attribute. I'm actually surprised it didn't raise an exception, but it seems it simply opted to treat the tag as plaintext instead. Regardless, you need to do something like the following instead:



          <select 
          disabled="@(Model.Id != 0 ? "disabled" : null)"
          asp-for="QuestionType"
          asp-items="@Html.GetEnumSelectList<Enums.QuestionTypes>()"
          id="form-type" data-parsley-required class="form-control">
          </select>


          If the value of an attribute is null, Razor will remove it, so you'll end up with disabled="disabled" if Model.Id is not equal to 0, and no disabled attribute at all if it is. You don't need to do anything crazy like the answers in the linked question suggest.



          EDIT



          Thinking about it more, I think it only went through because you broke the tag into multiple lines, so basically Razor treated the surrounding tag as invalid HTML and honed in on the line it could parse (the ternary expression). I'd imagine if you put the tag in a single line, you would in fact get an exception. Either way, it's a syntax error.






          share|improve this answer
























          • Sorry, I only broke it up into multiple lines for readability for the post. In my actual HTML it is all in one line. I checked my Chrome debugger as well as my logs (using boilerplate framework) and unfortunately no errors were thrown. Also, using your example, the html still is not built from the tag helpers and I get the same result as above where the compiled html has the tag helpers as attributes. I think I need to look at how the html is parsed in the Tag Helper class to see what's really going on.

            – Jordan Lee Burnes
            Jan 3 at 19:13






          • 1





            I can confirm that the error is not because it was written in multiple lines. I was able to perfectly reproduce this problem with a single-line tag-helper such as input.

            – ShadowKras
            Jan 3 at 19:22











          • Actually @ChrisPratt, your solution worked perfectly, I just got the syntax mixed up when testing it so thanks for that! It is much quicker than writing my own tag helper for sure!

            – Jordan Lee Burnes
            Jan 3 at 19:22








          • 1





            After some testing, earlier today, I managed to find out that the tag helper isnt even fired (as in, the Process method is never called, nor any of the tag helper properties ever defined) when reproducing the issue. So, we need to know why that happens, as there are no clues in the tag helper code.

            – ShadowKras
            Jan 3 at 19:27











          • @Chris Pratt This is a fine answer to the other question.

            – ShadowKras
            Jan 3 at 19:34














          1












          1








          1







          You cannot use a Razor expression inside a tag itself, only within the quotes of an attribute. I'm actually surprised it didn't raise an exception, but it seems it simply opted to treat the tag as plaintext instead. Regardless, you need to do something like the following instead:



          <select 
          disabled="@(Model.Id != 0 ? "disabled" : null)"
          asp-for="QuestionType"
          asp-items="@Html.GetEnumSelectList<Enums.QuestionTypes>()"
          id="form-type" data-parsley-required class="form-control">
          </select>


          If the value of an attribute is null, Razor will remove it, so you'll end up with disabled="disabled" if Model.Id is not equal to 0, and no disabled attribute at all if it is. You don't need to do anything crazy like the answers in the linked question suggest.



          EDIT



          Thinking about it more, I think it only went through because you broke the tag into multiple lines, so basically Razor treated the surrounding tag as invalid HTML and honed in on the line it could parse (the ternary expression). I'd imagine if you put the tag in a single line, you would in fact get an exception. Either way, it's a syntax error.






          share|improve this answer













          You cannot use a Razor expression inside a tag itself, only within the quotes of an attribute. I'm actually surprised it didn't raise an exception, but it seems it simply opted to treat the tag as plaintext instead. Regardless, you need to do something like the following instead:



          <select 
          disabled="@(Model.Id != 0 ? "disabled" : null)"
          asp-for="QuestionType"
          asp-items="@Html.GetEnumSelectList<Enums.QuestionTypes>()"
          id="form-type" data-parsley-required class="form-control">
          </select>


          If the value of an attribute is null, Razor will remove it, so you'll end up with disabled="disabled" if Model.Id is not equal to 0, and no disabled attribute at all if it is. You don't need to do anything crazy like the answers in the linked question suggest.



          EDIT



          Thinking about it more, I think it only went through because you broke the tag into multiple lines, so basically Razor treated the surrounding tag as invalid HTML and honed in on the line it could parse (the ternary expression). I'd imagine if you put the tag in a single line, you would in fact get an exception. Either way, it's a syntax error.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 3 at 18:53









          Chris PrattChris Pratt

          161k22247311




          161k22247311













          • Sorry, I only broke it up into multiple lines for readability for the post. In my actual HTML it is all in one line. I checked my Chrome debugger as well as my logs (using boilerplate framework) and unfortunately no errors were thrown. Also, using your example, the html still is not built from the tag helpers and I get the same result as above where the compiled html has the tag helpers as attributes. I think I need to look at how the html is parsed in the Tag Helper class to see what's really going on.

            – Jordan Lee Burnes
            Jan 3 at 19:13






          • 1





            I can confirm that the error is not because it was written in multiple lines. I was able to perfectly reproduce this problem with a single-line tag-helper such as input.

            – ShadowKras
            Jan 3 at 19:22











          • Actually @ChrisPratt, your solution worked perfectly, I just got the syntax mixed up when testing it so thanks for that! It is much quicker than writing my own tag helper for sure!

            – Jordan Lee Burnes
            Jan 3 at 19:22








          • 1





            After some testing, earlier today, I managed to find out that the tag helper isnt even fired (as in, the Process method is never called, nor any of the tag helper properties ever defined) when reproducing the issue. So, we need to know why that happens, as there are no clues in the tag helper code.

            – ShadowKras
            Jan 3 at 19:27











          • @Chris Pratt This is a fine answer to the other question.

            – ShadowKras
            Jan 3 at 19:34



















          • Sorry, I only broke it up into multiple lines for readability for the post. In my actual HTML it is all in one line. I checked my Chrome debugger as well as my logs (using boilerplate framework) and unfortunately no errors were thrown. Also, using your example, the html still is not built from the tag helpers and I get the same result as above where the compiled html has the tag helpers as attributes. I think I need to look at how the html is parsed in the Tag Helper class to see what's really going on.

            – Jordan Lee Burnes
            Jan 3 at 19:13






          • 1





            I can confirm that the error is not because it was written in multiple lines. I was able to perfectly reproduce this problem with a single-line tag-helper such as input.

            – ShadowKras
            Jan 3 at 19:22











          • Actually @ChrisPratt, your solution worked perfectly, I just got the syntax mixed up when testing it so thanks for that! It is much quicker than writing my own tag helper for sure!

            – Jordan Lee Burnes
            Jan 3 at 19:22








          • 1





            After some testing, earlier today, I managed to find out that the tag helper isnt even fired (as in, the Process method is never called, nor any of the tag helper properties ever defined) when reproducing the issue. So, we need to know why that happens, as there are no clues in the tag helper code.

            – ShadowKras
            Jan 3 at 19:27











          • @Chris Pratt This is a fine answer to the other question.

            – ShadowKras
            Jan 3 at 19:34

















          Sorry, I only broke it up into multiple lines for readability for the post. In my actual HTML it is all in one line. I checked my Chrome debugger as well as my logs (using boilerplate framework) and unfortunately no errors were thrown. Also, using your example, the html still is not built from the tag helpers and I get the same result as above where the compiled html has the tag helpers as attributes. I think I need to look at how the html is parsed in the Tag Helper class to see what's really going on.

          – Jordan Lee Burnes
          Jan 3 at 19:13





          Sorry, I only broke it up into multiple lines for readability for the post. In my actual HTML it is all in one line. I checked my Chrome debugger as well as my logs (using boilerplate framework) and unfortunately no errors were thrown. Also, using your example, the html still is not built from the tag helpers and I get the same result as above where the compiled html has the tag helpers as attributes. I think I need to look at how the html is parsed in the Tag Helper class to see what's really going on.

          – Jordan Lee Burnes
          Jan 3 at 19:13




          1




          1





          I can confirm that the error is not because it was written in multiple lines. I was able to perfectly reproduce this problem with a single-line tag-helper such as input.

          – ShadowKras
          Jan 3 at 19:22





          I can confirm that the error is not because it was written in multiple lines. I was able to perfectly reproduce this problem with a single-line tag-helper such as input.

          – ShadowKras
          Jan 3 at 19:22













          Actually @ChrisPratt, your solution worked perfectly, I just got the syntax mixed up when testing it so thanks for that! It is much quicker than writing my own tag helper for sure!

          – Jordan Lee Burnes
          Jan 3 at 19:22







          Actually @ChrisPratt, your solution worked perfectly, I just got the syntax mixed up when testing it so thanks for that! It is much quicker than writing my own tag helper for sure!

          – Jordan Lee Burnes
          Jan 3 at 19:22






          1




          1





          After some testing, earlier today, I managed to find out that the tag helper isnt even fired (as in, the Process method is never called, nor any of the tag helper properties ever defined) when reproducing the issue. So, we need to know why that happens, as there are no clues in the tag helper code.

          – ShadowKras
          Jan 3 at 19:27





          After some testing, earlier today, I managed to find out that the tag helper isnt even fired (as in, the Process method is never called, nor any of the tag helper properties ever defined) when reproducing the issue. So, we need to know why that happens, as there are no clues in the tag helper code.

          – ShadowKras
          Jan 3 at 19:27













          @Chris Pratt This is a fine answer to the other question.

          – ShadowKras
          Jan 3 at 19:34





          @Chris Pratt This is a fine answer to the other question.

          – ShadowKras
          Jan 3 at 19:34




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54026425%2fwhy-is-my-razor-code-not-working-with-tag-helpers-in-asp-net-core%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          'app-layout' is not a known element: how to share Component with different Modules

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          WPF add header to Image with URL pettitions [duplicate]