Контроллер веб-приложения .NET для js - PullRequest
0 голосов
/ 25 октября 2018

Я создаю простое веб-приложение Asp.NET с платформой MVC.

Теперь у меня есть такая ситуация:

У меня есть 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>

И этот код не вызывает метод RefreshChartEcg в HomeController.cs

Как я могу это исправить?

1 Ответ

0 голосов
/ 25 октября 2018

Вы можете использовать Ajax-запрос для обращения к методу в контроллере.

jQuery

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

Non jQuery

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

Затем вы можете нажать методв контроллере вот так:

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

Надеюсь, это поможет.

jQuery posts

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...