Reputation: 2070
I have series of checkboxes that have values. Now what I want to do is when I click a button, I want to get all the values of the checked checkboxes and put it on the url string that I will be using. Here's some of my code.
<span><b>Status: </b></span>
No arrangement yet <input type="checkbox" name="scheduleType" value="@AppointmentStatus.NoSchedule"/>
Proposed <input type="checkbox" name="scheduleType" value="@AppointmentStatus.Proposed"/>
Scheduled <input type="checkbox" name="scheduleType" value="@AppointmentStatus.Scheduled"/>
Pend re-scheduling <input type="checkbox" name="scheduleType" value="@AppointmentStatus.RescheduledPending"/>
Completed <input type="checkbox" name="scheduleType" value="@AppointmentStatus.Complete"/>
Cancelled <input type="checkbox" name="scheduleType" value="@AppointmentStatus.Cancelled"/>
<span style="margin-left:50px;"><button id="btnSearch" class="btn btn-success"> Search </button></span>
Now onClick.
$('#btnSearch').click(function () {
var dateFrom = $(".dateFrom").val();
var dateTo = $(".dateTo").val();
window.location.href = "home/masterschedule?scheduleType=" + /*here goes the checkboxes*/ + "&dateFrom=" + dateFrom + "&dateTo=" + dateTo;
});
Here's my receiving action.
[InPrivate]
public ActionResult MasterSchedule(List<string> scheduleType ,string dateFrom = "", string dateTo = "")
{
var appointments = _appointmentRepository.All().ToList();
var viewModel = new MasterScheduleIndexViewModel();
if (dateFrom == "" || dateTo == "")
{
viewModel.Appointment = appointments;
viewModel.DocumentPackage = _documentPackageRepository.All().Where(a => a.AMGStatus == DocumentPackageStatus.Approved || a.DocumentPackageStatus == DocumentPackageStatus.Approved).ToList();
viewModel.AppointmentNotes = _appointmentNoteRepository.All().ToList();
viewModel.Users = _userRepository.All().ToList();
}
else
{
var startDate = DateTime.ParseExact(dateFrom, "d/M/yyyy", null);
var endDate = DateTime.ParseExact(dateTo, "d/M/yyyy", null);
viewModel.Appointment = appointments.Where(a => DateTime.ParseExact(a.AppointmentDate, "d/M/yyyy", null) >= startDate && DateTime.ParseExact(a.AppointmentDate, "d/M/yyyy", null) <= endDate).ToList();
}
return View(viewModel);
}
I'm expecting a List of strings from the url, but would it be better if it's an array? Any ideas? Thanks!
Upvotes: 0
Views: 1010
Reputation: 123739
You can use serialize i.e $(':checkbox[name="scheduleType"]').serialize()
This will take care of sending the scheduleType
in the querystring
Example(...?scheduleType=Proposed&scheduleType=RescheduledPending&scheduleType=Cancelled&dateFrom=...
)
And you can leave the List<string> scheduleType
as it is in your action parameter.
Try
window.location.href = "home/masterschedule?" +
$(':checkbox[name="scheduleType"]').serialize() +
"&dateFrom=" + dateFrom + "&dateTo=" + dateTo;
Update
Based on the comment you want to deserialize it to Enum List, you can just do:
In your check box cast them to (int)
as follows.
<input type="checkbox" name="scheduleType" value="@((int)AppointmentStatus.NoSchedule)"/>
and in your action change the type of your action argument to List<AppointmentStatus> scheduleType
or as List<int> scheduleType
(based on how you need it) instead of List<string> scheduleType
and you should be good to go.
Upvotes: 1
Reputation: 3965
Give your checkboxes a class and you can do that:
html:
<span><b>Status: </b></span>
No arrangement yet <input type="checkbox" name="scheduleType" value="@AppointmentStatus.NoSchedule" class="chk"/>
Proposed <input type="checkbox" class="chk" name="scheduleType" value="@AppointmentStatus.Proposed"/>
Scheduled <input type="checkbox" class="chk" name="scheduleType" value="@AppointmentStatus.Scheduled"/>
Pend re-scheduling <input class="chk" type="checkbox" name="scheduleType" value="@AppointmentStatus.RescheduledPending"/>
Completed <input class="chk" type="checkbox" name="scheduleType" value="@AppointmentStatus.Complete"/>
Cancelled <input class="chk" type="checkbox" name="scheduleType" value="@AppointmentStatus.Cancelled"/>
<span style="margin-left:50px;"><button id="btnSearch" class="btn btn-success"> Search </button></span>
jquery:
$('#btnSearch').click(function () {
var chkValues = '';
$('.chk').each(function(){
if($(this).prop('checked') == true){
chkValues += $(this).val();
}
});
});
Upvotes: 0