C# 如何在方法/操作结果中使用模型作为参数?
我试图在此方法中使用模型作为参数,但收到错误消息-“CreateGastaticsReport没有重载方法…”我知道这是错误的,但我不确定如何解决此问题。我需要使用模型作为参数来获取所需的所有数据。问题位于ActionResult“GetData/CreateGastaticsReport”中 这是我的控制器类:C# 如何在方法/操作结果中使用模型作为参数?,c#,.net,asp.net-mvc,return,C#,.net,Asp.net Mvc,Return,我试图在此方法中使用模型作为参数,但收到错误消息-“CreateGastaticsReport没有重载方法…”我知道这是错误的,但我不确定如何解决此问题。我需要使用模型作为参数来获取所需的所有数据。问题位于ActionResult“GetData/CreateGastaticsReport”中 这是我的控制器类: public class GAStatisticsController : Controller { //GET: /ShopStatistics/
public class GAStatisticsController : Controller
{
//GET: /ShopStatistics/
public ActionResult GetData()
{
return Json(CreateGAStatisticsReport(GAStatisticsListModel model), JsonRequestBehavior.AllowGet);
}
public ActionResult GAStatistics()
{
return View(new GAStatisticsListModel());
}
private List<GAStatistics> CreateGAStatisticsReport(GAStatisticsListModel model)
{
var serviceAccountEmail = "xxxxxxxxx@developer.gserviceaccount.com";
var certificate = new X509Certificate2(@"C:\Users\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 = "MyApp",
});
DateTime? startDateValue = (model.StartDate == null) ? null
: (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.StartDate.Value, _dateTimeHelper.CurrentTimeZone);
DateTime? endDateValue = (model.EndDate == null) ? null
: (DateTime?)_dateTimeHelper.ConvertToUtcTime(model.EndDate.Value, _dateTimeHelper.CurrentTimeZone).AddDays(1);
string start = model.StartDate.ToString();
model.StartDate = DateTime.ParseExact(start, "yyyy-MM-dd", CultureInfo.InvariantCulture);
string end = model.EndDate.ToString();
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;
}
}
公共类GAStatisticsController:控制器
{
//获取统计数据/
公共操作结果GetData()
{
返回Json(CreateGastaticsReport(GastaticsListModel模型),JsonRequestBehavior.AllowGet);
}
公共行动结果统计()
{
返回视图(新的GAStatisticsListModel());
}
私有列表CreateGAStatisticsReport(GAStatisticsListModel模型)
{
var serviceAccountEmail=”xxxxxxxxx@developer.gserviceaccount.com";
var certificate=new X509Certificate2(@“C:\Users\Desktop\NopCommerce\Presentation\Nop.Web\key.p12”,“notasecret”,X509keystrageFlags.Exportable);
var-credential=新的ServiceAccountCredential(
新ServiceAccountCredential.初始值设定项(serviceAccountEmail)
{
Scopes=new[]{AnalyticsService.Scope.Analytics}
}.FromCertificate(证书));
//创建服务。
//旋转探戈
var GoogleAnalyticsService=new AnalyticsService(new BaseClientService.Initializer())
{
HttpClientInitializer=凭证,
ApplicationName=“MyApp”,
});
DateTime?startDateValue=(model.StartDate==null)?null
:(DateTime?)\u dateTimeHelper.ConvertToutTime(model.StartDate.Value,\u dateTimeHelper.CurrentTimeZone);
DateTime?endDateValue=(model.EndDate==null)?null
:(DateTime?)\u dateTimeHelper.ConvertToutTime(model.EndDate.Value,\u dateTimeHelper.CurrentTimeZone).AddDays(1);
字符串start=model.StartDate.ToString();
model.StartDate=DateTime.ParseExact(开始,“yyyy-MM-dd”,CultureInfo.InvariantCulture);
字符串end=model.EndDate.ToString();
model.EndDate=DateTime.ParseExact(结束,“yyyy-MM-dd”,CultureInfo.InvariantCulture);
var request=GoogleAnalyticsService.Data.Ga.Get(“Ga:xxxxxxxx”,开始,结束,“Ga:visitors”);
//指定一些附加查询参数
request.Dimensions=“ga:日期”;
request.Sort=“-ga:date”;
request.MaxResults=10000;
//执行并获取查询结果
Google.api.Analytics.v3.Data.GaData d=request.Execute();
List ListGaVisitors=新列表();
foreach(变量行在d.Rows中)
{
GAStatistics=新的GAStatistics(第[0]行、第[1]行);
listgagvisitors.Add(gavisitors);
}
回访旅客;
}
}
您的调用应该是
return Json(CreateGAStatisticsReport(model), JsonRequestBehavior.AllowGet);
假设您有可用的模型变量
模型需要是GetData()操作的参数,然后将其声明为
public ActionResult GetData(GAStatisticsListModel model)
并确保数据被传递。我相信我考虑过这个问题,但遗漏了一些东西。返回时似乎合乎逻辑,我需要传递方法参数,而不是Parantese中的类型。需要在这个主题上做更多工作。谢谢!