jquery 如何基于单独的控制器方法从Razor视图中构造JavaScript函数?

s2j5cfk0  于 9个月前  发布在  jQuery
关注(0)|答案(1)|浏览(151)

我有一个单独的控制器方法,它构建一个HTML表,并在通过AJAX POST调用时创建到我的Razor视图中。
请参阅下面的控制器方法片段:

[HttpPost]
        [Route("SupervisorWhereaboutsReport/{company}/{teamName}")]
        public DataResponse SupervisorWhereaboutsReport(string company, string teamName)
        {
           .......

            if (!result.Success)
            {
                return new DataResponse()
                {
                    Success = false,
                    ErrorMessage = result.ErrorMessage
                };
            }

            string hdr = "<table class='table table-sm table-hover table-bordered w-100'>" +
                            "<thead class='thead-dark sticky-top'\" style=\"top: 60px;\"><tr><th>Status</th><th>Job No</th><th>Photos/Notes</th><th>R No</th><th>WR No</th><th>Position</th><th>Address</th><th>Team</th><th>Req End Date</th><th>Area</th>" +
                                "<th>County</th><th>Dig Contractor</th><th>Road Type</th><th>Traffic Mgt</th></tr></thead>" +
                            "<tbody>";

            if (company != "AFF")
                hdr = "<table class='table table-sm table-hover table-bordered w-100'>" +
                            "<thead class='thead-dark'><tr><th>Status</th><th>Job No</th><th>Photos/Notes</th><th>R No</th><th>WR No</th><th>Position</th><th>Address</th><th>Team</th><th>Req End Date</th><th>Road Type</th><th>Traffic Mgt</th></tr></thead>" +
                            "<tbody>";

            foreach (DataRow item in result.Data.Rows)
            {
                if (district != item["district"].ToString())
                {
                    if (district != null)
                        rptOut += "</tbody></table></br>";

                    rptOut += hdr;

                    district = item["district"].ToString();
                }
                if (company == "AFF")
                    rptOut += "<tr>"
                            + "<td>" + item["tpe"].ToString() + "</td>"
                            + "<td><a href='/Job?JobNo=" + ((int)item["JobNo"]).ToString() + "' target='_blank'>" + ((int)item["JobNo"]).ToString() + "</a></td>"
                            + "<td><i class=\"fas fa-comments\" onclick=\"ShowJobNotes()\" style=\"cursor:pointer\"></i></td>"
                            + "<td>" + item["RNo"].ToString() + "</td>"
                            + "<td>" + item["ClientJobNum"].ToString() + "</td>"
                            + "<td>" + item["position"].ToString() + "</td>"
                            + "<td>" + item["Addr"].ToString().Replace(", ,", ", ") + "</td>"
                            + "<td>" + item["Team"].ToString() + "</td>"
                            + "<td>" + ((DateTime)item["ReqComplDate"]).ToString("dd/MM/yyyy") + "</td>"
                            + "<td>" + item["district"].ToString() + "</td>"
                            + "<td>" + item["area"].ToString() + "</td>"
                            + "<td>" + item["Dig_Contractor"].ToString() + "</td>"
                            + "<td>" + item["RoadType"].ToString() + "</td>"
                            + "<td>" + item["TrafficMgtRef"].ToString() + "</td>"
                        + "</tr>";
                else

           .......
        }

字符串
我关注的代码行是:

+ "<td><i class=\"fas fa-comments\" onclick=\"ShowJobNotes()\" style=\"cursor:pointer\"></i></td>"


我的问题是,如何从控制器方法调用Razor视图中的ShowJobNotes()JavaScript函数?

@section Scripts{
    <script>
        function runReport() {
            var selectedTeam = $("#Team").val();
            var selectedCompany = $("#Companies").val();
              
        $("#isBusy").show();
            $.ajax({
                type: "POST",
                url: "/ReportsAPI/SupervisorWhereaboutsReport/" + selectedCompany + "/" + selectedTeam,
                data: "{}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: function (data, status, xhttp) {
                    if (!data.success) {
                        alert(data.ErrorMessage);
                        return;
                    }    //$("#rno_error").show();
                    //result
                    $("#divRpt").html(data.result);
                    console.log(data.result);

                $("#isBusy").hide();

                },
                failure: function (errMsg) {
                    console.log("FAILED!");
                    alert(errMsg);
                }
            });

        function ShowJobNotes(){
            window.open("/Job/Notes?jobno=" + $(".JobNumber").val()+"&fullname="+$("#FullName").val(), $(".JobNumber").val()+" Notes", "width=900,height=1000");
            return false;
        }
}
    </Script>
}


我不知道如何让它工作,因为控制器方法实际上不是我的页面模型的一部分。它是一个单独的文件,存储了一堆在各种相关类中使用的端点。尝试在我的网页上调用该函数会导致ShowJobNotes()is not defined错误。

koaltpgm

koaltpgm1#

在您的@section {..}中,添加以下内容

$(document).on('load','.fas.fa-comments',function(e){
  ShowJobNotes();
 //or.. 
 $('.fas.fa-comments').trigger('click');
});

字符串

相关问题