0

I have a form that I need to modify to pass a date to if a date exists in a date picker. The form is retrieved via JavaScript and data is passed to a MVC controller method.

I'm doing this in my click function to get the form.

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

Here is the javascript for my button:

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

My function looks like this:

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

        }
    });
}

And here is my MVC controller method:

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

How can I pass in a date if a date exists? Thanks.

1 Answer 1

1

I can see from your js function that you're using get. So if you want to pass it to controller just add date param to your url like this at the begin of your function:

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

Or just pass it to function:

FormGet('dashboard/delayedspiff?endDate=' + endDate, 'delayedspiff')
Sign up to request clarification or add additional context in comments.

7 Comments

teo van kot, I'm brand new to JavaScript. Where do I put this line of code? url = url + "?endDate=" + $("#your-date-id");
Well, It's not working. The date is not being passed to the controller.
@hollyquinn check in debug what's an actuall url that passed?
@hollyquinn depending on your date format could be that you should use encodeURIComponent() js function to format date right way
minor but you can try ... +$("startDate").val()
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.