Jquery C#MVC控制器传递空日期

Jquery C#MVC控制器传递空日期,jquery,asp.net,asp.net-mvc,twitter-bootstrap,datepicker,Jquery,Asp.net,Asp.net Mvc,Twitter Bootstrap,Datepicker,我有一个ASP.NETMVC应用程序。它使用引导导航栏在两个窗体之间导航。我正在将日期从日期选择器传递到C#函数,但如果没有日期输入到日期选择器中,则在页面加载时它不起作用。如果没有日期,我不知道如何补偿。如果datepicker中没有日期,则应使用今天的日期。我没有收到错误消息,只是不起作用。以下是我的看法: <div class="row-fluid 1"> <div class="col-lg-12 delayed_spiff_body">

我有一个ASP.NETMVC应用程序。它使用引导导航栏在两个窗体之间导航。我正在将日期从日期选择器传递到C#函数,但如果没有日期输入到日期选择器中,则在页面加载时它不起作用。如果没有日期,我不知道如何补偿。如果datepicker中没有日期,则应使用今天的日期。我没有收到错误消息,只是不起作用。以下是我的看法:

<div class="row-fluid 1">
    <div class="col-lg-12 delayed_spiff_body">

        <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 type="text" id="startDate" 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="button" value="Submit" />
                </div>
            </div>
        </div> 
<div class="row spiff_tabs_body">
            <!-- Nav tabs -->

            <ul class="nav nav-tabs spiff_tabs" role="tablist">
                <li role="presentation" class="active">
                    <a href="#home" aria-controls="home" role="tab" data-id="delayedspiff" data-toggle="tab" onclick="FormGet('dashboard/delayedspiff', 'delayedspiff')">Potential Spiff</a>
                </li>
                <li role="presentation">
                    <a href="#profile" aria-controls="profile" role="tab" data-id="instantspiff" data-toggle="tab" onclick="FormGet('dashboard/instantspiff', 'delayedspiff')">Instant Spiff</a>
                </li>
            </ul>
            <!-- Tab panes -->
            <div class="tab-content">
                <div role="tabpanel" class="tab-pane active" id="delayedspiff"></div>
                <div role="tabpanel" class="tab-pane" id="instantspiff"></div>
            </div>
        </div>
    </div>
</div>

<script>
    $(function () {
    FormGet('dashboard/delayedspiff', 'delayedspiff');
});
</script>

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

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

   } else {
       if (correctId == "delayedspiff")
       {          
           $.get("@Url.Action("DelayedSpiff", "Dashboard")", { startDate: startDate });

       } else if (correctId = "instantspiff") {

           $.get("@Url.Action("InstantSpiffDate", "Dashboard")", { startDate: startDate });
       }    
   }         
});
</script>

$(函数(){ FormGet('dashboard/delayedspiff','delayedspiff'); }); $(“.spiffdate btn”)。单击(函数(){ var correctId=$(“ul.spiff_tabs li.active a”).attr('data-id'); console.log(correctId); var startDate=$(“#startDate”).val(); 如果(起始日期==“”){ }否则{ 如果(correctId==“delayedspiff”) { $.get(“@Url.Action(“DelayedSpiff”,“Dashboard”),{startDate:startDate}); }否则如果(correctId=“instantspiff”){ $.get(“@Url.Action(“instantspifdate”,“Dashboard”),{startDate:startDate}); } } });
这是我控制器中的部分代码,应该有日期传递给它

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

        //var startDate = DateTime.Today.AddDays(-6);

        if (startDate == DateTime.MinValue)
        {
            startDate = DateTime.Today.AddDays(-6);
        }
        else
        {
            startDate = startDate.AddDays(-7);
        }            

        var acctId = User.AccountID;

        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); 
public ActionResult DelayedSpiff(DateTime startDate)
{
可用变量=_appService.GetFeatureStatus(1,“spiffDashboard”);
如果(!可用)
返回RedirectToAction(“DatabaseDown”,“Error”,new{area=”“});
//var startDate=DateTime.Today.AddDays(-6);
if(startDate==DateTime.MinValue)
{
startDate=DateTime.Today.AddDays(-6);
}
其他的
{
startDate=startDate.AddDays(-7);
}            
var acctId=User.AccountID;
var endDate=DateTime.Today.AddDays(1);//1
Dictionary dict=新字典();
尝试
{
var属性=新字典
{
{“Type”,“DelayedSpiff”}
};
遥测.跟踪事件(“仪表板”,属性);
dict=_reportingService.GetDailyDelayedSpiffSummaries(acctId、startDate、endDate);

有人能告诉我我做错了什么吗?我猜这与FormGet函数有关。谢谢。

如果输入是空的,那么代码显式不执行任何操作:

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

}

如果您想调用服务器端操作,而不考虑输入的内容,那么只需完全删除该条件,并执行当前在
else
块中的逻辑。

您能在操作中使startDate为null吗

public ActionResult DelayedSpiff(DateTime? startDate)
然后在代码中处理这个问题

if (!startDate.HasValue || startDate.Value == DateTime.MinValue)
    {
        startDate = DateTime.Today.AddDays(-6);
    }
    else
    {
        startDate = startDate.Value.AddDays(-7);
    }   
然后你可以移除

if (startDate == "") {

} else {

David,它仍然不起作用。如果我去掉javascript代码中的那些行,它根本不会加载表单。@hollyquinn:你能再澄清一下这个问题吗。“根本不加载表单”不是真正的描述性。会发生什么?具体来说,你在这里尝试做什么,失败在哪里?当然。会发生的是使用引导导航选项卡加载页面。根据你在哪个选项卡上,它会显示不同的表单。当表单加载actionresult DelayedSpiff时,它会运行并用数据填充一个表。我屏幕上有一个日期选择器,允许用户选择一个日期并显示该日期的一系列数据。如果日期选择器中没有日期,则表单应为今天的日期加载。我遇到的问题是,如果加载表单时日期选择器中没有日期,则它不会运行操作结果DelayedSpiff。这是否会使se?谢谢。@hollyquinn:你是在谈论这个函数调用吗?:
FormGet('dashboard/delayedspiff','delayedspiff');
你还没有展示
FormGet()无论哪种方式,您都必须在这里至少进行一些调试。这里没有人可以为您这样做。是什么客户端代码发出AJAX请求?是请求发出的吗?服务器的响应是什么?如果没有发出,是什么阻止它发出?嗨,JamieD77。我在startDate=startDate.AddDays(-7)上收到一个错误;它表示错误13“System.Nullable”不包含“AddDays”的定义,并且找不到接受“System.Nullable”类型的第一个参数的扩展方法“AddDays”(是否缺少using指令或程序集引用?)您可以创建一个新变量,也可以始终使用startDate.Value.AddDaysOk,但现在在这一行dict=\u reportingService.GetDailyDelayedSpiffSummaries(acctId、startDate、endDate);我收到错误消息error 22参数2:无法从'System.DateTime'转换为'System.DateTime'相同的东西..使用startDate.Value我在发布我的评论后立即发现了这一点。:)它工作正常!非常感谢!