2016-12-08 48 views
0

我正在开发一个.NET MVC应用程序。 在我的.cshtml文件,我已经在我的文本框的一个提供简单的日历工具如下:在C中验证HTML日历工具#

<input class="form-control" id="createdOn" type="text" /> 

下面是它的JS脚本:

$(document).ready(function() 
{ 
     $("#createdOn").datepicker(); 
} 

现在我要检查用户是否已经在上面提到的文本框中选择了任何日期,或者不在我的C#文件中。

我有一个LINQ查询,它检索数据并存储在名为“query”的变量中。

所以在这个C#文件中的代码是这样的:

var query = (from //and so on 
    select { 
     // data to fetch 
} 
//below is where I'm stuck 
if (!string.IsNullOrEmpty(sc.CreatedOn))  // here sc is an entity 
{ 
     query.Where(w => w.CreatedOn == sc.CreatedOn); 
} 

return query.ToList(); 
} 

所以这里IF块似乎并不正确验证CreatedOn以上。 我在做什么错? 任何人都可以告诉更好的方法来验证它吗?

+0

在上面的代码'sc'中是obje实体搜索的ct。在此搜索类CreatedOn声明并具有字符串的数据类型 –

回答

1

首先,检查而不是你的sc.CreatedOn是已经在你的实体中的DateTime。

如果是,那么你可能需要申请另一种方法来检查CreatedOn的存在,

正常时间字段

DateTime dat = new DateTime(); 

if (dat==DateTime.MinValue) 
{ 
    //unassigned 
} 

,并在情况下,日期时间

DateTime? dat = null; 

if (!dat.HasValue) 
{ 
    //unassigned 
} 

更多信息来自: How to check if a DateTime field is not null or empty?

+0

谢谢@ SKLTFZ。 –