gpt4 book ai didi

c# - HttpPost 未执行

转载 作者:行者123 更新时间:2023-12-01 07:57:44 33 4
gpt4 key购买 nike

我有一个带有 httpPost 属性的 ActionResult。如果我没有记错的话,当用户从 DateTimePicker 中选择日期时,我需要此属性。就我而言,我有 2 个日期时间选择器,类型为 @html.EditorFor。这些日期选择器代表我的 View 模型中的 DateTime 属性 StartDate 和 EndDate。单击“GAStatisticsReport-Submit”按钮后,这些日期应该能够在 Controller 和 CreateGAStatisticsReport 方法中使用,因为 CreateGAStatisticsReport 将模型作为参数。但是,在我选择 StartDate 和 EndDate 后,什么也没有发生。

如果我从 ActionResult GetData 中删除 HttpPost 属性,该方法将按预期运行,但 StartDate 和 EndDate 将为 Null。我不知道问题是否出在模型绑定(bind)、HttpPost、html.BeginForm 或提交按钮的 javascript 逻辑上。尝试过各种各样的事情。

我在这里缺少什么?

创建GAStatisticsList模型:

[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;

}

View (问题可能出在 View 中,需要告诉按钮存储在 DateTimePickers 中选择的日期,以便我可以在 Controller 中使用它们):

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

对所有文字表示抱歉。任何帮助将不胜感激。

最佳答案

使用 $.get( 将发出 HttpGet 请求。代码中的目标是 '/GAStatistics/GetData',它是GAStatistics Controller 中的 GetData 操作。但是,该方法标有 [HttpPost],因此不会暴露给 get 请求。

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

关于c# - HttpPost 未执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22772401/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com