C#MVC控制器传递空日期

时间:2015-11-18 14:51:59

标签: jquery asp.net asp.net-mvc twitter-bootstrap datepicker

我有一个ASP.net MVC应用程序。它使用引导导航栏在两个表单之间导航。我将日期选择器中的日期传递给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>

这是我控制器中代码的一部分,应该将日期传递给它。

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

谁能告诉我自己做错了什么?我猜测它与FormGet函数有关。感谢。

2 个答案:

答案 0 :(得分:2)

你能在你的行动中让startDate成为可空吗?

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 {

答案 1 :(得分:0)

好吧,如果输入为空,那么您的代码显式什么都不做:

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

}

如果要调用服务器端操作而不管输入的内容如何,​​则只需完全删除该条件并执行当前位于else块中的逻辑。