asp.net-mvc如何使用模型作为参数的方法/一个?

标签: Asp.net-MVC .Net C#
发布时间: 2016/10/9 6:10:35
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我试着去使用模型作为此方法的参数但是我得到错误消息-""CreateGAStatisticsReport 没有重载方法......"我知道这是错的但不知道如何解决这个问题。我需要使用模型作为参数来获得所需的所有数据。问题位于一个"GetData/CreateGAStatisticsReport"。

这里是我的控制器类︰

 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;

        }
    }

解决方法 1:

应该是你调用

return Json(CreateGAStatisticsReport(model), JsonRequestBehavior.AllowGet);

假设你已经模型变量可用。

模型需要将参数的 getdata () 行动,然后将其声明为

public ActionResult GetData(GAStatisticsListModel model)

并确保传递的数据。

官方微信
官方QQ群
31647020