Javascript将日期传递给MVC控制器

时间:2015-11-30 15:00:22

标签: javascript jquery asp.net-mvc asp.net-mvc-4

我有一个表单需要修改,以便在日期选择器中存在日期时传递日期。表单通过JavaScript检索,数据传递给MVC控制器方法。

我在点击功能中执行此操作以获取表单。

FormGet('dashboard/delayedspiff', 'delayedspiff')

以下是我的按钮的javascript:

<script>    
   $(".spiffdate-btn").click(function(){
       var correctId = $("ul.spiff_tabs li.active a").attr('data-id');


       var endDate = $("#startDate").val();
       if (endDate == "") {

       } else {
           if (correctId == "delayedspiff")
           {
               FormGet('dashboard/delayedspiff', 'delayedspiff')

} else if (correctId = "instantspiff") {
               FormGet('dashboard/instantspiff', 'delayedspiff')

}           
       }     
   });   
</script>

我的功能如下:

function FormGet(url, divid, formid, event) {

    if (event !== undefined) {
        event.preventDefault();
    }
    if (formid !== undefined) {
        $('#' + formid).remove();
    }
    Loading(divid);
    $.get(siteRoot + url, function (data) {
        $('#' + divid).html(data);
        if (divid === 'containerbody') history.pushState("", document.title, url);
        if (formid !== undefined) {
            var form = $('#' + formid).removeData("validator").removeData("unobtrusiveValidation");
            $.validator.unobtrusive.parse(form);

        }
    });
}

这是我的MVC控制器方法:

 public ActionResult DelayedSpiff(DateTime? endDate)
        {
            DateTime startDate = DateTime.Today.AddDays(-6);
            var available = _appService.GetFeatureStatus(1, "spiffDashboard");
            if (!available)
                return RedirectToAction("DatabaseDown", "Error", new { area = "" });


            if (!endDate.HasValue || endDate.Value == DateTime.MinValue)
            {
                endDate = DateTime.Today.AddDays(1);
            }
            else
            {
                startDate = endDate.Value.AddDays(-6);
                endDate = endDate.Value.AddDays(1);
            }           

            var acctId = User.AccountID;



            Dictionary<DateTime, List<SpiffSummaryModel>> dict = new Dictionary<DateTime,List<SpiffSummaryModel>>();

            try
            {
                var properties = new Dictionary<string, string>
                {
                    { "Type", "DelayedSpiff" }
                };
                telemetry.TrackEvent("Dashboard", properties);

                dict = _reportingService.GetDailyDelayedSpiffSummaries(acctId, startDate, endDate.Value);

            }
            catch (Exception e)
            {
                if (e.InnerException is SqlException && e.InnerException.Message.StartsWith("Timeout expired"))
                {
                    throw new TimeoutException("Database connection timeout");
                }
                var error = _errorCodeMethods.GetErrorModelByTcError(PROJID.ToString("000") + PROCID.ToString("00") + "001", "Exception Getting DelayedSpiff Dashboard View", PROJID, PROCID);
                error.ErrorTrace = e.ToString();
                _errorLogMethods.LogError(error);
                return RedirectToAction("index", "error", new { error = error.MaskMessage });
            }

            var spiffDateModels = new List<DelayedSpiffDateModel>();

            foreach (var entry in dict)
            {
                var spiffDateModel = new DelayedSpiffDateModel();

                spiffDateModel.Date = entry.Key;

                spiffDateModel.Carriers = new List<DelayedSpiffCarrierModel>();

                foreach (var item in entry.Value)
                {
                    var spiffCarrierModel = new DelayedSpiffCarrierModel();
                    spiffCarrierModel.Carrier = item.CarrierName;
                    spiffCarrierModel.CarrierId = item.CarrierId;
                    spiffCarrierModel.ApprovedSpiffTotal = item.ApprovedSpiffTotal;
                    spiffCarrierModel.EligibleActivationCount = item.EligibleActivationCount;
                    spiffCarrierModel.IneligibleActivationCount = item.IneligibleActivationCount;
                    spiffCarrierModel.PotentialSpiffTotal = item.PotentialSpiffTotal;
                    spiffCarrierModel.SubmittedActivationCount = item.SubmittedActivationCount;
                    spiffCarrierModel.UnpaidSpiffTotal = item.UnpaidSpiffTotal;
                    spiffDateModel.Carriers.Add(spiffCarrierModel);
                }

                spiffDateModels.Add(spiffDateModel);
            }
            spiffDateModels = spiffDateModels.OrderByDescending(x => x.Date).ToList();

            return PartialView(spiffDateModels);
        }

如果日期存在,我如何传递日期?感谢。

1 个答案:

答案 0 :(得分:1)

我可以从你的js函数中看到你正在使用get。因此,如果您想将它传递给控制器​​,只需在您的函数开头将日期参数添加到您的网址:

url = url + "?endDate=" + $("#startDate").val();

或者只是将其传递给函数:

FormGet('dashboard/delayedspiff?endDate=' + endDate, 'delayedspiff')