-1
我有一個功能中的複選框下拉菜單。所以用戶可以選擇一個,多個或零個。當用戶選擇一個或多個時,一切都是正確的。但是當用戶不選擇任何我有一個空指針異常。爲了處理這個異常,我希望默認情況下,如果他沒有選擇任何東西來顯示所有的結果(就像他正在選擇所有的結果一樣)。我怎樣才能做到這一點?在複選框中獲取默認值
這裏是JS功能
$(function() {
$("#datepicker").datepicker({
//defaultDate: "+1w",
changeMonth: true,
changeYear: true,
numberOfMonths: 1,
minDate: "01/01/2008"
})
.datepicker('setDate', new Date());
$("button.action").click(function() {
//var users = new Array();
var date = $('#datepicker').val().toString();
var selected_values = new Array();
//var userName = $(' .checked:checked').val();
$(document).ready(function() {
selected_values = []; // initialize empty array
$(".checked:checked").each(function() {
selected_values.push($(this).val());
});
});
$.ajax({
url: 'EmployeeDate',
datatype: "application/json",
traditional:true,
data: {
lstUserName: selected_values,
strDate: date
},
success: function (data, textStatus, jqXHR) {
$('#DataUser').html(data);
},
error: function() {
console.log("error handler when ajax request fails... ");
},
});
});
});
,這是控制器:
public IEnumerable<DateTime> getInfoByDate(string strDate, string[] lstUserName)
{
CareDB context = new CareDB();
IEnumerable<DateTime> lst = null;
List<DateTime> model = new List<DateTime>();
if (lstUserName != null)
{
foreach (string user in lstUserName)
{
SqlParameter userName = new SqlParameter("@EmployeeName", user);
SqlParameter Date = new SqlParameter("@Date", strDate);
object[] parameters = new object[] { Date, userName };
model.Add(context.ReleaseDate.SqlQuery("_UserInformationByDate @Date, @EmployeeName", parameters).ToList().FirstOrDefault());
}
}
else
{
SqlParameter Date = new SqlParameter("@Date", strDate);
SqlParameter userName = new SqlParameter("@EmployeeName", lstUserName);
object[] parameters = new object[] { Date, userName };
model = context.ReleaseDate.SqlQuery("_UserInformationByDate @Date, @EmployeeName", parameters).ToList();
}
context.Dispose();
context = null;
return model;
}