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.