1
public ActionResult Create(RecurringTask recurringTask, FormCollection collection, ICollection<string> dayOfTheWeek)

I am trying to loop through the dayOfTheWeek (which is a group of checkboxes) and I am trying to find out which one is true so than I can use that to assemble a string ex:Monday, Tuesday, etc.

I am just having trouble finding a way of looping through my collection to do it. I keep getting can't apply == to of type string to bool error.

var days = dayOfTheWeek.ToString();
                    foreach (string day in dayOfTheWeek)
                    {
                        if(day == true)
                        {

                        }
                    }
                    recurringTask.DaysOfTheWeek = days;

This is what I am thinking on how to do it. But I imagine someone out there has a way better idea than I do. The day == true gives me that string to bool error and its obvious to why its happening, I just don't know how to get around it.

My view is this:

<input type="checkbox" name="dayOfTheWeek" value="Monday" />
<input type="checkbox" name="dayOfTheWeek" value="Tuesday" />
<input type="checkbox" name="dayOfTheWeek" value="Wednesday" />
<input type="checkbox" name="dayOfTheWeek" value="Thursday" />
<input type="checkbox" name="dayOfTheWeek" value="Friday" />
<input type="checkbox" name="dayOfTheWeek" value="Saturday" />
<input type="checkbox" name="dayOfTheWeek" value="Sunday" />
2
  • 2
    Are you checking the checkbox's Caption property, or its Checked property? My guess is you aren't checking the Checked property, because if you do if(MyCheckbox.Checked) that works fine. Commented Sep 12, 2011 at 19:49
  • @SpikeX - I haven't even thought of that. I'm a super newb Commented Sep 12, 2011 at 19:56

3 Answers 3

2

public ActionResult Create(RecurringTask recurringTask, FormCollection collection, ICollection dayOfTheWeek)

Sorry to say it but that's probably one of the worst action signatures I have ever seen. A mixture of a domain model, a FormCollection and some ICollection<string>.

Use view models, strongly typed views and editor templates (that's probably the 10^4th time I am writing this sentence on StackOverflow in response to questions in the asp.net-mvc tag)! They will make your life so much easier. So a list of days and a corresponding boolean property to indicate whether this day is selected:

public class MyViewModel
{
    public IEnumerable<DayOfWeekViewModel> DaysOfWeek { get; set; }

    ... put any other properties that you consider useful for this view
}

public class DayOfWeekViewModel
{
    public string DayOfWeek { get; set; }
    public bool IsSelected { get; set; }
}

then a controller:

public class HomeController : Controller
{
    public ActionResult Index()
    {
        var model = new MyViewModel
        {
            DaysOfWeek = CultureInfo
                .CurrentCulture
                .DateTimeFormat
                .DayNames
                .Select(x => new DayOfWeekViewModel
                {
                    DayOfWeek = x,
                })
        };
        return View(model);
    }

    [HttpPost]
    public ActionResult Index(MyViewModel model)
    {
        // model.DaysOfWeek will contain all you need here
        // TODO: do some processing
        // here you can loop through model.DaysOfWeek to identify which 
        // days have been selected and take respective actions
        // ...

        // once you have finished processing you could redirect
        return RedirectToAction("success");
    }
}

then a corresponding view:

@model MyViewModel

@using (Html.BeginForm())
{
    ... you could put any other fields from your view model that
        will be used by this form here

    @Html.EditorFor(x => x.DaysOfWeek)
    <input type="submit" value="OK" />
}

and the corresponding editor template (~/Views/Home/EditorTemplates/DayOfWeekViewModel.cshtml):

@model DayOfWeekViewModel

<div>
    @Html.CheckBoxFor(x => x.IsSelected) @Html.DisplayFor(x => x.DayOfWeek)
    @Html.HiddenFor(x => x.DayOfWeek)
</div>
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks buddy, I'm new to all of this. Getting stuff like this really helps me out
2

The error message is revealing. You are trying to compare a string day to true or false. What does it mean for a string to be true or false?

You really want to see if the checkboxes are checked. You claim that dayOfTheWeek is a group of CheckBoxes, but you are calling ToString() on it, which would convert it to a list of strings. What is the code before var days = dayOfTheWeek.ToString(); Where are you declaring dayOfTheWeek?

Once you really have a List<Checkbox>, you really want to iterate through that list to see if each element is checked.

foreach(CheckBox cb in dayOfTheWeek)
{
    if(cb.Checked)
    {
      // Logic
    }
}

3 Comments

I don't think this works in ASP.NET MVC because there usually is no CheckBox object.
The reason why I am calling a ToString on it is because I wan't to assemble a string after I find out if the checkbox is true/false and than save it as a string
List<Checkbox>? -1. That's ASP.NET MVC, not classic WebForms.
2

If you named your checkboxes "dayOfTheWeek", ICollection<string> dayOfTheWeek only contains the selected checkboxes.

So you could just take this list as "result", but I would encourage you to filter the list of days with a list of valid values to prevent malicious attacks.

Comments

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.