2014-03-31 50 views
1

我有httpPost -attribute一个ActionResult。如果我没有弄错,当用户从DateTimePicker中选择一个日期时,我需要这个属性。在我的情况下,我有2个Datetimepickers AV类型@ html.EditorFor。这些日期选择器代表我的视图模型中的DateTime属性StartDate和EndDate。这些日期suposed能够在控制器和CreateGAStatisticsReport方法使用点击“GAStatisticsReport提交”按钮CreateGAStatisticsReport有模型parameeter后。然而,在我选择了StartDate和EndDate后,什么也没有发生。HttpPost不执行

如果我从的ActionResult的GetData除去HttpPost属性的方法如预期除了起始日期和结束日期将是空运行。我不知道如果问题是模型绑定,HttpPost,html.BeginForm或提交按钮的JavaScript逻辑。试过各种各样的事情。

我在这里错过了什么?

CreateGAStatisticsListModel:

[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 = "[email protected]"; 
     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; 

    } 

视图(这个问题可能是在这里的观点,需要告诉按钮存储DateTimePickers中选择日期以便我可以在控制器中使用它们):

@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> 
    } 

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

回答

3

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

要么使用$.post([HttpGet]庆祝动作。

+0

谢谢,我现在可以执行的按钮。然而,HttpPost属性似乎没有按照预期的那样工作.StartDate和model.EndDate在选择日期之后没有用anny DateTime值填充并单击提交。我只是空了 – koffe14