0

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"/> &nbsp;&nbsp;&nbsp;
        Proposed <input type="checkbox" name="scheduleType" value="@AppointmentStatus.Proposed"/>&nbsp;&nbsp;&nbsp;
        Scheduled <input type="checkbox" name="scheduleType" value="@AppointmentStatus.Scheduled"/>&nbsp;&nbsp;&nbsp;
        Pend re-scheduling <input type="checkbox" name="scheduleType" value="@AppointmentStatus.RescheduledPending"/>&nbsp;&nbsp;&nbsp;
        Completed <input type="checkbox" name="scheduleType" value="@AppointmentStatus.Complete"/>&nbsp;&nbsp;&nbsp;
        Cancelled <input type="checkbox" name="scheduleType" value="@AppointmentStatus.Cancelled"/>
        <span style="margin-left:50px;"><button id="btnSearch" class="btn btn-success">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Search&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</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!

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

12 Comments

Thank you for this suggestion but I have a problem here. The first value has "scheduleType=" string on it. The rest is good.
@ljpv14 You mean in my example right? I don't know what you have there so just typed in. Also remember that you don't have to append scheduleType= in the query string as you have in your original code
I tried serializing and received it as a list of string successfully. The problem is, the first element containts this string. "scheduleType=Scheduled", other string are good without the scheduleType= string in it.
@ljpv14 That is because you are not using the code i have. You dont need to prefix "scheduleType=
And also, I need to get their values, not their strings. I'm expecting numbers, not words.
|
0

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"/> &nbsp;&nbsp;&nbsp;
        Proposed <input type="checkbox" class="chk" name="scheduleType" value="@AppointmentStatus.Proposed"/>&nbsp;&nbsp;&nbsp;
        Scheduled <input type="checkbox" class="chk" name="scheduleType" value="@AppointmentStatus.Scheduled"/>&nbsp;&nbsp;&nbsp;
        Pend re-scheduling <input class="chk" type="checkbox" name="scheduleType" value="@AppointmentStatus.RescheduledPending"/>&nbsp;&nbsp;&nbsp;
        Completed <input class="chk" type="checkbox" name="scheduleType" value="@AppointmentStatus.Complete"/>&nbsp;&nbsp;&nbsp;
        Cancelled <input class="chk" type="checkbox" name="scheduleType" value="@AppointmentStatus.Cancelled"/>
        <span style="margin-left:50px;"><button id="btnSearch" class="btn btn-success">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Search&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</button></span>

jquery:

$('#btnSearch').click(function () {
     var chkValues  = '';
    $('.chk').each(function(){
        if($(this).prop('checked') == true){
            chkValues += $(this).val();
        }
    });
 });

1 Comment

Just an fyi... jquery takes care of this with serialize() method. Also you are missing the key scheduleType

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.