7 seconds
7 seconds

Reputation: 133

MVC (Razor) Syntax in JavaScript

In my razor view i have below code :

@foreach (var rma in Model)
{
<tr>
    <td>
        @{
            if (rma.AntalRMA == rma.Antal.Remove(rma.Antal.Length - 5))
            {
                <button id="btnequls" type="button" class="btn" data-toggle="tooltip" data-placement="top" title="" data-container="body" data-original-title="some text">text here</button>
            }
            else
            {
                <button onclick="location.href = '/Account/RMA/@[email protected]&[email protected]';" type="button" id="btnflere" class="btn">text </button>
            }
        }
    </td>
</tr>

And then, when I tried do samething in my JavaScript, I can see Tooltip in if is not working and href inside button in else is not working too. Can anyone please help me or point me in the right direction! Thanks in advance :)

JavaScript:

<script>
    let skip = 0;
    function SearchRMA(append, searching) {
        var amount = 10;
        skip += amount;
        var LikeOrderNummer = $("#SearchBox").val();

        $.ajax({
            type: "POST",
            dataType: "json",
            cache: false,
            url: "/Account/RMAHistorik",
            data: {
                LikeOrderNummer: LikeOrderNummer,
                From: CurrentPage * amount,
                Amount: amount,
                SearchRMA: true,
                Searching: searching,
                Skip: skip

            },
            success: function (result) {
                if ($.trim(result)) {

                    var ResultString = "";

                    $.each(result, function (i, e) {
                        ResultString +=
                            '<tr>' +
                            '<td>' + dateFormat(new Date(parseInt((e.RMASendDato).match(/\d+/)[0]))) + '</td>' +
                            '<td>' + '<a target="_blank" title="@Html.Raw(MitEdData.Resources.IndexTexts.Vis_sag)" href ="RMADetails?id=' + e.Id + '">' + "Sag nr:." + "&nbsp" + e.Id + '</a></td>' +
                            '<td>' + e.Ordrenummer + '</td>' +
                            '<td>' + e.Fakturnummer + '</td>' +
                            '<td>' + e.AntalRMA + ' @Html.Raw(MitEdData.Resources.IndexTexts.stk)' + '</td>' +
                            '<td>' + e.Antal +  '</td>' +
                            '<td>'; // <-- here is my problem
                        if (e.AntalRMA == e.Antal) {
                            ResultString += '<button id="btnequls" type="button" class="btn" data-toggle="tooltip" data-placement="top" title="" data-container="body" data-original-title="Du har brugt alt dit antal RMA til denne sag">Opret flere sager</button>'
                        }
                        else {
                            ResultString += '<button onclick="location.href = RMA/' + e.Fakturnummer + '?Varenummer=' + e.Varenummer + '&Ordreno=' + e.Ordrenummer + '" type="button" id="btnflere" class="btn">Opret flere sager </button>'
                        } // <-- here is my problem too

                        ResultString += '</td>' +
                           '<td>' + e.Varenummer + '</td>' +
                            '<td>';

                        if (e.Status == "Sag i afventning") {
                            ResultString += '<i style="color:#f3bb45;" class="fa fa-exclamation-triangle" aria-hidden="true"></i>' +
                                e.Status;
                        }
                        else if (e.Status == "Sag oprettet") {
                            ResultString += '<i style="color:#34c834;" class="fa fa-pencil-square-o" aria-hidden="true"></i>' +
                             e.Status;
                        }
                        else if (e.Status == "Under behandling") {
                            ResultString += '<i style="color:rgba(220,77,70,1); font-size: 18px;" class="fa fa-exclamation-triangle" aria- hidden="true"></i>' +
                            e.Status;
                        }
                        else if (e.Status == "Afventer vare") {
                            ResultString += '<i style="color:#34c834; font-size:18px;" class="fa fa-clock-o" aria- hidden="true"></i>' +
                            e.Status;
                        }
                        else if (e.Status == "Returpakkelabel fremsendt") {
                            ResultString += '<i style="color:#34c834; font-size:18px;" class="fa fa-exchange" aria- hidden="true"></i>' +
                            e.Status;
                        }
                        else if (e.Status == "Modtaget vare") {
                            ResultString += '<i style="color:#34c834; font-size:18px;" class="fa fa-archive" aria- hidden="true"></i>' +
                            e.Status;
                        }
                        else if (e.Status == "Afventer tilbagemelding fra kunde") {
                            ResultString += '<i style="color:#34c834; font-size:18px;" class="fa fa-clock-o" aria- hidden="true"></i>' +
                            e.Status;
                        }
                        else if (e.Status == "Sendt til værksted") {
                            ResultString += '<i style="color:#34c834; font-size:18px;" class="fa fa-truck" aria- hidden="true"></i>' +
                            e.Status;
                        }
                        else if (e.Status == "Afsluttet (vare returneret)") {
                            ResultString += '<i style="color:#34c834; font-size:18px;" class="fa fa-check" aria- hidden="true"></i>' +
                            e.Status;
                        }
                        else if (e.Status == "Afsluttet (vare krediteret)") {
                            ResultString += '<i style="color:#34c834; font-size:18px;" class="fa fa-check" aria- hidden="true"></i>' +
                            e.Status;
                        }

                        ResultString += '</td>' + '</tr>'
                    });
                }
                else {
                    $("#notfound").html("<div class='alert alert-danger'><i class='fa fa-exclamation-triangle' aria-hidden='true'></i> @Html.Raw(MitEdData.Resources.IndexTexts.Ingen_match_fundet______)</div>");
                }

                if (append) {
                    $("#ResultRMA").append(ResultString);
                }
                else {
                    $("#ResultRMA").html(ResultString);
                    $("#BackList").show();
                    $("#Next").hide();
                }

                //Back Button disbaled
                if (window.history && window.history.pushState) {

                    history.pushState("nohb", null, "");
                    $(window).on("popstate", function (event) {
                        if (!event.originalEvent.state) {
                            history.pushState("nohb", null, "");
                            return;
                        }
                    });
                }

                function dateFormat(d) {
                    console.log(d);
                    return (d.getDate() + "").padStart(2, "0")
                        + "-" + ((d.getMonth() + 1) + "").padStart(2, "0")
                        + "-" + d.getFullYear();
                }
            }
        })
    }
</script>

Tooltip JS :

$('[data-toggle="tooltip"], [rel="tooltip"]').tooltip();

Upvotes: 0

Views: 94

Answers (2)

erntay2
erntay2

Reputation: 140

Replace this part

ResultString += '</td>' +

              '</tr>'

with this

ResultString += '</td>' +

              '</tr>';

$("#TableId tbody").append(Resultstring); //append your ResultString into the table
$('[data-toggle="tooltip"], [rel="tooltip"]').tooltip(); //initialize again the tooltip for the button

Upvotes: 0

Vishal Khatal
Vishal Khatal

Reputation: 119

You need to append Resultstring to Table.for eg.

$("#TableName").append(Resultstring);

Upvotes: 1

Related Questions