简体   繁体   中英

Mixing razor code with HTML

Hi my mark up has snippets like below

<thead>
    <tr class="">
        <th data-field="firstname">First Name</th>

        @{

            foreach (MapDetail geMapDetailHead in Model.mapDetails)
            {
                string firstText, secText, thirdText;

                if (geMapDetailHead.ResultTypeIDs.Equals("-9999"))
                {

                 foreach (string rt in geMapDetailHead.ResultTypeIDs.Split(','))
                    {
                 firstText = @geMapDetailHead.Name;

                 string tab = geMapDetailHead.year;

                 int? month = geMapDetailHead.Month != 0 ? geMapDetailHead.Month : (geMapDetailHead.mapheader.Month != 0 ? geMapDetailHead.mapheader.Month : 0);

                  //switch (month.GetValueOrDefault()) 
                    //{
                    //    default:
                    //        tab += "";
                    //        break;
                    //    case 1:
                    //        tab += " Jan";
                    //        break;
                    //    case 2:
                    //        tab += " Feb";
                    //        break;
                    //    case 3:
                    //        tab += " Mar";
                    //        break;
                    //    case 4:
                    //        tab += " Apr";
                    //        break;
                    //    case 5:
                    //        tab += " May";
                    //        break;
                    //    case 6:
                    //        tab += " Jun";
                    //        break;
                    //    case 7:
                    //        tab += " Jul";
                    //        break;
                    //    case 8:
                    //        tab += " Aug";
                    //        break;
                    //    case 9:
                    //        tab += " Sep";
                    //        break;
                    //    case 10:
                    //        tab += " Oct";
                    //        break;
                    //    case 11:
                    //        tab += " Nov";
                    //        break;
                    //    case 12:
                    //        tab += " Dec";
                    //        break;
                    //}

                    //secText = tab;

                   <th id=@geMapDetailHead.MapDetailID>@firstText @secText</th>

                }  @*end for loop*@
             } 
          }  @*end for loop*@
         }
      </tr>

</thead>

As soon as i un comment the switch statement it stops recognizing tag as markup. i have also tried putting

       @:<th id=@geMapDetailHead.MapDetailID>@firstText @secText</th>

but did not work. How can i mix both the code and markup?

Well turned out that i had applied 1 extra @ in the following line

firstText = @geMapDetailHead.Name;

I changed it to

firstText = geMapDetailHead.Name;

and BINGO!! Cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM