Asp.net MVC在点击按钮时运行javascript
Asp.net MVC run javascript on button click
我的代码逻辑有点乱,我不知道如何修复它。我有一个Bootstrap导航选项卡面板,当单击选项卡时,它会根据单击的选项卡在我的控制器中运行MVC C#函数。实际上,我需要在点击按钮时实现这一点。因此,用户在日期选择器中输入日期,单击提交,然后根据选择的选项卡运行函数。我如何在点击按钮时做到这一点?
这是我的日期选择器和按钮:
<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="startDate" type="text" class="form-control" />
<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>
这是我的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);
var startDate = $('#startDate').val();
if (correctid == "delayedspiff")
$.get("@Url.Action("DelayedSpiffDate", "Dashboard")", { startDate: startDate });
else
$.get("@Url.Action("InstantSpiffDate", "Dashboard")", { startDate: startDate });
});
</script>
如果需要的话,这里是我的控制器:
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);
}
关于如何在点击按钮时实现这一点,有什么想法吗?
您可以尝试创建"click"事件的处理程序,该处理程序应该检索所选选项卡的有效标识符,并向服务器发送GET请求。
$(".spiffdate-btn").click(function(){
var correctid = $(".tab-content .active").attr("id");
var startDate = $("#startDate").val();
if (correctid == "delayedspiff")
$.get("@Url.Action("DelayedSpiffDate", "Dashboard")", { startDate: startDate });
else
$.get("@Url.Action("InstantSpiffDate", "Dashboard")", { startDate: startDate });
});
相关文章:
- 将文本框链接到由按钮运行的javascript公式
- 从页面javascript按钮运行Node.JS
- 在同一页面上单击按钮运行不同的代码
- 如何阻止一个Submit按钮运行Submit Click功能
- 根据选中的按钮运行不同的计算时,设置单选按钮
- 单击javascript中的按钮运行python脚本
- 链接按钮运行的代码无效
- 从按钮运行函数
- 为什么获胜't我的HTML按钮运行我的JavaScript函数
- 使用html按钮运行python脚本
- 点击一个按钮运行一个功能-不工作
- 点击按钮运行Javascript
- Chrome扩展赢得't点击按钮运行功能
- 如何使图像按钮运行PHP脚本
- 单击按钮运行函数并添加Class
- 点击浏览器返回按钮运行动画
- 如何从HTML中的一个按钮运行整个Javascript页面?
- 选择按钮运行功能
- 点击html按钮运行PHP函数
- 如何使一个按钮运行php代码与ajax,无需刷新页面购物车