HttpPost not executing

本文关键字:executing not HttpPost | 更新日期: 2023-09-27 17:55:25

我有一个带有httpPost -attribute的ActionResult。如果我没记错的话,当用户从日期时间选择器中选择日期时,我需要此属性。就我而言,我有 2 个日期时间选择器 av 类型 @html。编者。这些日期选取器表示我的视图模型中的日期时间属性"开始日期"和"结束日期"。在单击"GAStatisticsReport-Submit"按钮后,这些日期应该能够在控制器和CreateGAStatisticsReport方法中使用,因为CreateGAStatisticsReport将模型作为参数。但是,在我选择了"开始日期"和"结束日期"之后,没有任何反应。

如果我从操作结果获取数据中删除 HttpPost 属性,该方法将按预期运行,但 StartDate 和 EndDate 将为 Null。我不知道问题是否是模型绑定,HttpPost,html。BeginForm 或提交按钮的 javascript 逻辑。尝试了各种各样的事情。

我在这里错过了什么?

创建GAStatisticsListModel:

[NopResourceDisplayName("Admin.GAStatistics.GAStatistics.StartDate")]
        [UIHint("DateNullable")]
        public DateTime? StartDate { get; set; }
        [NopResourceDisplayName("Admin.GAStatistics.GAStatistics.EndDate")]
        [UIHint("DateNullable")]
        public DateTime? EndDate { get; set; }

GaStatisticsController (当我点击提交按钮时,GetData 会运行):

 [HttpPost]
    public ActionResult GetData(GAStatisticsListModel model)
    {
        return Json(CreateGAStatisticsReport(model), JsonRequestBehavior.AllowGet);
    }

    public ActionResult GAStatistics()
    {
        return View(new GAStatisticsListModel());
    }

    public List<GAStatistics> CreateGAStatisticsReport(GAStatisticsListModel model)
    {
        var serviceAccountEmail = "xxxxxxxxxxxxxx@developer.gserviceaccount.com";
        var certificate = new X509Certificate2(@"C:'Users'user'Desktop'NopCommerce'Presentation'Nop.Web'key.p12", "notasecret", X509KeyStorageFlags.Exportable);

        var credential = new ServiceAccountCredential(
        new ServiceAccountCredential.Initializer(serviceAccountEmail)
        {
            Scopes = new[] { AnalyticsService.Scope.Analytics }
        }.FromCertificate(certificate));
        // Create the service.
        //Twistandtango
        var GoogleAnalyticsService = new AnalyticsService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = "Twist",
        });
        string start = model.StartDate.ToString(); //<----- The model date values are needed here
        model.StartDate = DateTime.ParseExact(start, "yyyy-MM-dd", CultureInfo.InvariantCulture);
        string end = model.EndDate.ToString(); //<----- The model date values are needed here
        model.EndDate = DateTime.ParseExact(end, "yyyy-MM-dd", CultureInfo.InvariantCulture);
        var request = GoogleAnalyticsService.Data.Ga.Get("ga:xxxxxxxx", start, end, "ga:visitors");
        //Specify some addition query parameters
        request.Dimensions = "ga:date";
        request.Sort = "-ga:date";
        request.MaxResults = 10000;
        //Execute and fetch the results of our query
        Google.Apis.Analytics.v3.Data.GaData d = request.Execute();

        List<GAStatistics> ListGaVisitors = new List<GAStatistics>();
        foreach (var row in d.Rows)
        {
            GAStatistics GaVisits = new GAStatistics(row[0], row[1]);
            ListGaVisitors.Add(GaVisits);
        }

        return ListGaVisitors;
    }

视图(问题可能出在视图中,需要告诉按钮将选择的日期存储在日期时间选择器中,以便我可以在控制器中使用它们):

  @model GAStatisticsListModel
    @using Nop.Admin.Models.GAStatistics;
    @using Telerik.Web.Mvc.UI;
    @using Nop.Admin.Controllers;
    @using Telerik.Web.Mvc.UI.Html;
    @using System.Web.Mvc;
    @using System.Linq;
    @{
        ViewBag.Title = "GAStatistics";
        Layout = "~/Administration/Views/Shared/_AdminLayout.cshtml";
    }
    @using (Html.BeginForm())
    {
        <h2>Google Analytics Statistic Reports</h2>

    <table class="adminContent">
             <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.StartDate):
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.StartDate)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.EndDate):
                </td>
                <td class="adminData">
                    @Html.EditorFor(model => model.EndDate)
                </td>
            </tr>
            <tr>
                <td class="adminTitle">
                    @Html.NopLabelFor(model => model.GAStatisticsId ):
                </td>
                <td class="adminData">
                    @Html.DropDownList("GAStatisticsId", Model.AvailableGAStatistics)
                    <input type="button" id="GAStatisticsReport-Submit" class="t-button" value="@T("Admin.Common.Search")" />
            </tr>
    </table>
    <div class="t-widget t-grid">
      <table cellspacing="0">
        <thead class="t-grid-header">
          <tr>
            <th class="t-header" scope="col">
              <span class="t-link">Area Chart</span>
            </th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>
              <div id="chart_div1" style="width: 900px; height: 500px;"></div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="t-widget t-grid">
      <table cellspacing="0">
        <thead class="t-grid-header">
          <tr>
            <th class="t-header" scope="col">
              <span class="t-link">Line Chart</span>
            </th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>
              <div id="chart_div2" style="width: 900px; height: 500px;"></div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>
    <div class="t-widget t-grid">
      <table cellspacing="0">
        <thead class="t-grid-header">
          <tr>
            <th class="t-header" scope="col">
              <span class="t-link">Column Chart</span>
            </th>
          </tr>
        </thead>
        <tbody>
          <tr>
            <td>
              <div id="chart_div4" style="width: 900px; height: 500px;"></div>
            </td>
          </tr>
        </tbody>
      </table>
    </div>

    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript" src="/Scripts/jquery.min.js"></script>
    <script type="text/javascript">

        //Submit button
        $('#GAStatisticsReport-Submit').click(function () {
            var grid = $('#GetData').data('tGrid');
            function onDataBinding(e) {
                var searchModel = {
                    StartDate: $('#@Html.FieldIdFor(model => model.StartDate)').val(),
                EndDate: $('#@Html.FieldIdFor(model => model.EndDate)').val(),
            };
            e.data = searchModel;
        }

        $("#GAStatisticsReport-Submit").click(function () {
            if ($("select[name='GAStatisticsId'] option:selected").text() == "Visitors")
                drawChart()

        })
        google.load("visualization", "1", { packages: ["corechart"] });
        google.load("visualization", "1", { packages: ["treemap"] });
        function drawChart() {
            $.get('/GAStatistics/GetData', {},
                function (data) {
                    var tdata = new google.visualization.DataTable();
                    tdata.addColumn('date', 'Date');
                    tdata.addColumn('number', 'Visitors');
                    for (var i = 0; i < data.length; i++) {
                        var dateStr = data[i].Date.substr(0, 4) + "-" + data[i].Date.substr(4, 2) + "-" + data[i].Date.substr(6, 2);
                        tdata.addRow([new Date(dateStr), parseInt(data[i].Visitors)]);
                    }
                    var options = {
                        title: "Antal unika besökare per datum"
                    };
                    var chart1 = new google.visualization.AreaChart(document.getElementById('chart_div1'));
                    var chart2 = new google.visualization.LineChart(document.getElementById('chart_div2'));
                    var chart4 = new google.visualization.ColumnChart(document.getElementById('chart_div4'));
                    chart1.draw(tdata, options);
                    chart2.draw(tdata, options);
                    chart4.draw(tdata, options);
                });
        }
    </script>
    }

对不起所有的文字。安妮的帮助将不胜感激。

HttpPost not executing

使用 $.get( 将发出HttpGet请求。代码中的目标是'/GAStatistics/GetData',即GAStatistics控制器中的操作GetData。但是,该方法标有 [HttpPost],因此不会公开以获取请求。

使用$.post(或使用 [HttpGet] 标记操作。