locked
Problem for using Asp.net c# and html mixing code RRS feed

  • Question

  • User1700320598 posted

    I tried to use below code in asp.net but there is error in html tags like and and say it could not find begin tag. but begin tag is in first if. the errors are in between line 44 to 52.

    @{
    
            string n = "";
    
            int j = 0;
    
    foreach (var item in Model.RelatedProduct){
                if (item.ProductId != n)
                    j = 1;
                else
                    j = 0;            
                if(j==1){
                    <div class="modal fade" id="myModal" role="dialog">
                        <div class="modal-dialog modal-dialog-centered " role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                    <h5 class="modal-title">@item.CatalogDescription</h5>
                                    <button class="close" type="button" data-dismiss="modal">
                                        <span aria-hidden="true">&times;</span>
                                    </button>
                                </div>
                                <div class="modal-body">
                                    <table class="table table-hover">
                                        <thead class="thead-dark">
                                            <tr>
                                                <td>Customer</td>
                                            </tr>
                }
                @if(j == 1 || j == 0){
                                            <tr>
                                                <td>@item.CustomerName</td>
                                                <td>@item.Price</td>
                                            </tr>
                }
                @if(j==1){ 
                                    </table>
                                </div>
                                <div class="modal-footer">
                                    <button class="btn" data-dismiss="modal">Cancel</button>
                                </div>
                            </div>
                        </div>
                    </div>
                }
            }
        }
    Saturday, May 2, 2020 11:08 PM

All replies

  • User-474980206 posted

    The markup in an if must have balanced and matching tags. So 

     @If (true){
       <div>
      }

    is not legal markup. You need to restructure your ifs so the markup is balanced inside. Also a <tr> with a parent table is invalid. You just need break it into the zero case and the one case.

    Sunday, May 3, 2020 12:03 AM
  • User711641945 posted

    Hi mehdi4467,

    It seems you want to add a `cancel` button when j=1 but j!=0.If so,I suggest that you could change like below:

    @{
        string n = "";
        int j = 0;
        foreach (var item in Model.RelatedProduct)
        {
            if (item.ProductId != n)
                j = 1;
            else
                j = 0;
            if (j == 1)
            {
                <div class="modal fade" id="myModal" role="dialog">
                    <div class="modal-dialog modal-dialog-centered " role="document">
                        <div class="modal-content">
                            <div class="modal-header">
                                <h5 class="modal-title">@item.CatalogDescription</h5>
                                <button class="close" type="button" data-dismiss="modal">
                                    <span aria-hidden="true">&times;</span>
                                </button>
                            </div>
                            <div class="modal-body">
                                <table class="table table-hover">
                                    <thead class="thead-dark">
                                        <tr>
                                            <td>Customer</td>
                                        </tr>
                                        }
                                        @if (j == 1 || j == 0)
                                        {
                                            <tr>
                                                <td>@item.CustomerName</td>
                                                <td>@item.Price</td>
                                            </tr>
                                        }
    
                                </table>
                            </div>
                            @if (j == 1)
                            {
                                <div class="modal-footer">
                                    <button class="btn" data-dismiss="modal">Cancel</button>
                                </div>
                            }
                        </div>
                        </div>
                    </div>
            }
        }
    }

    Best Regards,

    Rena

    Tuesday, May 5, 2020 2:26 AM