首页 > 解决方案 > .NET Web 应用程序控制器到 js

问题描述

我正在使用 MVC 框架构建一个简单的 Asp.NET Web 应用程序。

现在我有这种情况:

我有这样的 HomeController.cs:

using AnalisiHRVElaborazioni.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Services;

namespace AnalisiHRVElaborazioni.Controllers
{
    public class HomeController : Controller
    {
        public int _tipoElaborazioneTimeDomain = 1;
        public int _tipoElaborazioneNonlinear = 4;
        public int _tipoElaborazionePoincare = 3;
        OmniacareRehabDemDataProductionEntities dbData = new OmniacareRehabDemDataProductionEntities();

        public ActionResult TimeDomain()
        {
            ViewBag.ArrayTimeDomain = getTimeDomainTable(1, _tipoElaborazioneTimeDomain, 6).ToList();
            ViewBag.ArrayGeometric = getTimeDomainTable(1, _tipoElaborazioneTimeDomain,7).ToList();
            return View();
        }

        public ActionResult FreqDomain()
        {
            //RECUPERO DAL DB IL WELCH BURG E LOMB SCARLAGE
            //1 per il momento è statico, dopo sarà dinamico sarà sostituito dal idRRSlotFiltri
            //il secondo 1 = Welch PSD
            //2 = Burg PSD
            //3 = Lomb PSD
            ViewBag.ArrayWelch = getFrequencyDomainTable(1,1).ToList();
            ViewBag.ArrayBurg = getFrequencyDomainTable(1, 2).ToList();
            ViewBag.ArrayLomb = getFrequencyDomainTable(1, 3).ToList();
            return View();
        }

        public ActionResult Nonlinear()
        {
            ViewBag.ArrayEntropy = getTimeDomainTable(1, _tipoElaborazioneNonlinear, 4).ToList();
            ViewBag.ArrayDFA = getTimeDomainTable(1, _tipoElaborazioneNonlinear,5).ToList();

            return View();
        }

        public ActionResult Poincare()
        {
            ViewBag.Array = getTimeDomainTable(1, _tipoElaborazionePoincare, 6).ToList();
            return View();
        }

        public ActionResult TimeFrequency()
        {
            ViewBag.ArrayWelch = getTimeFrequencyTable(1, 1).ToList();
            ViewBag.ArrayBurg = getTimeFrequencyTable(1, 2).ToList();
            ViewBag.ArrayLomb = getTimeFrequencyTable(1, 3).ToList();
            return View();
        }


        [WebMethod]
        public static Array RefreshChartEcg(string columnname)
        {
            /**
             * qui devo recuperare il codice per recuperare le informazioni real time dal database
             * */
            int lastID = 0;

            return null;
        }



        [NonAction]
        public IQueryable<ParametroDTO> getTimeDomainTable(int idFiltri, int idTipoElaborazione,int idTipoParametro)
        {
            return from f in dbData.RRSlotFiltriXDatiAggregatiElaborati  
                   join um in dbData.PHR_CCD_Units on f.idUm equals um.ID into sr
                   from x in sr.DefaultIfEmpty()
                   join p in dbData.RRParametri on f.idParametro equals p.ID
                   where f.IdRRSlotFiltri == idFiltri && f.idTipoElaborazione == idTipoElaborazione
                   && f.idTipoParametro == idTipoParametro
                   select new ParametroDTO
                   {
                       um = sr.FirstOrDefault().Code,
                       value = f.valore,
                       nome = p.Descrizione

                   };
        }




        [NonAction]
        public IQueryable<FreqDomainDTO> getFrequencyDomainTable(int idFiltri, int idTipoParametro)
        {
            /**
             * 1 = Welch
             * 2 = Burg
             * 3 = Lomb scargle
             * */
            OmniaCareRehabDemProductionEntities db = new OmniaCareRehabDemProductionEntities();
            return from f in db.AA_V_RRFiltriDatiAggregatiElaboratiFD
                   join p in db.AA_V_RRParametri on f.idParametro equals p.ID 
                   where f.IdRRSlotFiltri == idFiltri && f.idTipoParametro == idTipoParametro
                   select new FreqDomainDTO
                   {
                       nome = p.Descrizione,
                       lfhf = f.valore5,
                       peak = f.valore1,
                       powerMs = f.valore2,
                       powerPerc = f.valore3,
                       powerNu = f.valore4
                   };
        }

        [NonAction]
        public IQueryable<FreqDomainDTO> getTimeFrequencyTable(int idFiltri, int idTipoParametro)
        {
            /**
             * 1 = Welch
             * 2 = Burg
             * 3 = Lomb scargle
             * */
            OmniaCareRehabDemProductionEntities db = new OmniaCareRehabDemProductionEntities();
            return from f in db.AA_V_RRFiltriDatiAggregatiElaboratiTF
                   join p in db.AA_V_RRParametri on f.idParametro equals p.ID
                   where f.IdRRSlotFiltri == idFiltri && f.idTipoParametro == idTipoParametro
                   select new FreqDomainDTO
                   {
                       nome = p.Descrizione,
                       lfhf = f.valore5,
                       peak = f.valore1,
                       powerMs = f.valore2,
                       powerPerc = f.valore3,
                       powerNu = f.valore4
                   };
        }


    }
}

我有 5 个视图(FreqDomain.cshtml、Nonlinear.cshtml、Poincare.cshtml、TimeDomain.cshtml、TFrequency.cshtml)

在此页面的一个中,我想显示一个从数据库中获取系列的图表,因此我在 TimeDomain.cshtml 下构建此代码:

<script>
                    zingchart.THEME = "classic";

                    var colors = {
                        blue: "rgba(151,187,205,1)",
                        gray: "#EBEBEB",
                        grayDark: "#3F3F3F"
                    };


                    function randomVal(min, max, num) {
                        var aData = [];
                        for (var i = 0; i < num; i++) {
                            var val = ((Math.random() * (max - min)) + min);
                            aData.push(parseInt(val));
                        }
                        return aData;
                    }

                    var myConfig = {
                        type: 'bar',
                        backgroundColor: "#FFF",
                        plot: {
                            lineColor: "rgba(151,187,205,1)",
                            lineWidth: "2px",
                            backgroundColor2: "rgba(151,187,205,1)",
                            marker: {
                                backgroundColor: "rgba(151,187,205,1)",
                                borderColor: "white",
                                shadow: false
                            }
                        },
                        plotarea: {
                            backgroundColor: "white"
                        },
                        scaleX: {
                            lineColor: colors.gray,
                            lineWidth: "1px",
                            tick: {
                                lineColor: "#C7C7C7",
                                lineWidth: "1px"
                            },
                            guide: {
                                lineStyle: 'solid',
                                lineColor: colors.gray,
                                alpha: 1
                            },
                            item: {
                                color: colors.grayDark
                            }
                        },
                        scaleY: {
                            lineColor: colors.gray,
                            lineWidth: "1px",
                            tick: {
                                lineColor: "#C7C7C7",
                                lineWidth: "1px"
                            },
                            guide: {
                                lineStyle: 'solid',
                                lineColor: colors.gray,
                                alpha: 1
                            },
                            item: {
                                color: colors.grayDark
                            }
                        },
                        series: [{
                            values: randomVal(0, 2, 8),
                            lineColor: "rgba(220,220,220,1)",
                            lineWidth: "2px",
                            alpha: 0.5,
                            borderWidth: "2px",
                            borderColor: "#C7C7C7",
                            borderTop: "2px solid #C7C7C7",
                            borderBottom: "0px",
                            backgroundColor1: "rgba(220,220,220,1)",
                            backgroundColor2: "rgba(220,220,220,1)",
                            marker: {
                                backgroundColor: "rgba(220,220,220,1)",
                            }

                        }]
                    }


                    zingchart.render({
                        id: 'myChart',
                        data: myConfig,
                        hideprogresslogo: true,
                    })

                    //real-time feed random math function
                    window.feed = function (callback) {
                        var Data = JSON.stringify({ columnname: "colonna" });
                        $.ajax({
                            type: "POST",
                            url: "RefreshChartEcg",
                            //data: '{name: "' + $("#<%=txtLastID.ClientID%>")[0].value + '" }',
                            //data : '{lastID : "10"}',
                            data: Data,
                            contentType: "application/json; charset=utf-8",
                            dataType: "json",
                            success: function (response) {
                                /*var tick = {};
                                tick.plot0 = response.d;
                                tick.plot1 = parseInt(10 + 90 * Math.random(), 10);
                                callback(JSON.stringify(tick));*/

                                var i = 0;
                                for (i = 0; item = response.d[i]; i++) {
                                    //alert(item.Valore);
                                    var tick = {};
                                    tick.plot0 = item.Valore;
                                    //tick.plot1 = parseInt(10 + 90 * Math.random(), 10);
                                    callback(JSON.stringify(tick));
                                }
                                //inserisco nella textbox, l'ultimo ID
                                ;

                                if (number == 1) {
                                    $("#txtLastID").attr("value", response.d[i - 1].ID);
                                    var ID = $("#textIdOmnia").val();
                                    var element = { ID: ID, Message: "500" };

                                    window.parent.postMessage(element, "*");
                                    //window.top.postMessage(element, '*');
                                    //window.contentWindow.postMessage("messaggio2");
                                    //alert(window.parent.ID);
                                    number = number + 1;
                                }


                            },
                            failure: function (response) {
                                alert(response.d);
                            }
                        });


                        function OnSuccesss(response) {
                            alert(response.d);
                        }
                    }
                </script>

而且这段代码没有调用 HomeController.cs 下的 RefreshChartEcg 方法

我该如何解决?

标签: c#asp.net-mvc

解决方案


您可以使用 Ajax 请求来访问控制器中的方法。

jQuery

$.ajax({
   type: "POST",
   url: "/Home/FreqDomain",
   cache: false
});

非 jQuery

const Http = new XMLHttpRequest();
Http.open("POST", "/Home/FreqDomain");
Http.send();

然后你可以像这样在控制器中点击一个方法:

[HttpPost]
public IActionResult FreqDomain()
{
   //do logic here  
}   

希望这可以帮助。

jQuery 帖子


推荐阅读