hollyquinn
hollyquinn

Reputation: 652

Passing DatePicker value via Javavscript to C# Controller

I have a bootstrap datapicker, that I need to pass the value using javascript to my C# code in my controller. I know how to call the function inside the controller with javascript, but I am unsure how to pass the value of the datepicker. This all needs to happen on a button click. Here's the code for my datepicker:

<div class="row spiff-datepicksection">
            <div class="col-lg-6 pull-right">
                <div class="col-sm-5 col-lg-offset-4">
                    <div class="form-group">
                        <div class="input-group date">
                            <input id="datetimepicker1" type="text" class="form-control" id="startDate" />
                            <span class="input-group-addon">
                                <span class="glyphicon glyphicon-calendar"></span>
                            </span>
                        </div>
                    </div>
                </div>
                <div class="col-lg-3">
                    <input class="spiffdate-btn" type="submit" value="Submit" />
                </div>
            </div>
        </div>

Here is my javascript:

 <script>
    $('a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
        var wrongid = $('.tab-content .active').attr('id');
        $('a[data-toggle="tab"]').removeClass("active");
        $(this).addClass("active"); 
        var correctid = $(this).data("id"); 
        alert($('.tab-content .active')[0].outerHTML); 
        if (correctid == "delayedspiff")
            $.get("@Url.Action("DelayedSpiffDate", "Dashboard")");
        else 
            $.get("@Url.Action("InstantSpiffDate", "Dashboard")");

    });
</script>

Here's my controller:

 public ActionResult DelayedSpiffDate(DateTime startDate)
    {
        var available = _appService.GetFeatureStatus(1, "spiffDashboard");
        if (!available)
            return RedirectToAction("DatabaseDown", "Error", new { area = "" });

        var acctId = User.AccountID;
        startDate = DateTime.Today.AddDays(-6);  // -6
        var endDate = DateTime.Today.AddDays(1); // 1

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

        }
        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);
    }

currently the startdate is set to datetime.today. I need it to take the date from the datepicker. Let me know if any further information is needed. Thanks.

Upvotes: 3

Views: 2727

Answers (1)

JB06
JB06

Reputation: 1931

In your javascript, add this line to get the value of the datepicker textbox (you have two id attributes on your datepicker by the way):

var startDate = $('#startDate').val();

Then pass it along with your $.get calls like so:

$.get('@Url.Action("DelayedSpiffDate", "Dashboard")', { startDate: startDate });

Upvotes: 3

Related Questions