3

In my web application I have a page with a list of "Requests" that an administrator can either Approve or Deny in bulk. So the page would look something like:

[x] Request 1
[ ] Request 2
[x] Request 3
[ ] Request 4

[Approve Selected]   [Deny Selected]

My model looks something like:

public class RequestManagementModel
{
    public List<string> LicenseIdsToProcess { get; set; }
    public List<Resource> Resources { get; set; }
    // additional fields
}

And in my view, I have:

int counter = 0;
@foreach (Resource r in Model.Resources)
{
    <tr>
        <td>
            @Html.CheckBoxFor(model => model.LicenseIdsToProcess[counter++], new { value = r.RequestId })
        </td>
    </tr>
}

And of course my controller action accepts the model on form post

public ActionResult ProcessRequests(RequestManagementModel model, ProcessType type)
{
    // process requests here
}

So, as you might expect, I'm getting an error in the view on model.LicenseIdsToProcess[counter++] that says "Cannot implicitly convert type 'string' to 'bool'". It doesn't like the fact that I'm trying to use checkboxes to represent a list of values that a user can select one or many from, rather than a single true/false.

I'd like this to be set up so that, for every checkbox the user selects, that string id value is bound to the list of id's in my model when the form is posted. I know how to do it just by using <input type="checkbox">, because I can set the value of the checkbox there. But is there a way to use it with Html.CheckBoxFor to achieve strong typing through model binding?

Thanks.

1 Answer 1

4

Your list needs to be:

public class RequestManagementModel
{
    public Dictionary<string, bool> LicenseIdsToProcess { get; set; }
    public List<Resource> Resources { get; set; }
    // additional fields
}

Since LicenseIdsToProcess is currently a List<string> Html.CheckBoxFor() cannot convert the string value into a bool value. You need to use something like a Dictionary, or a List eg

public class Licence
{
    public string Id { get; set; }
    public bool Processed { get;set; }
}

public class RequestManagementModel
{
    public List<Licence> LicenseIdsToProcess { get; set; }
    public List<Resource> Resources { get; set; }
    // additional fields
}

int counter = 0;
@foreach (Resource r in Model.Resources)
{
    <tr>
        <td>
            @Html.CheckBoxFor(model => model.License[counter++].Processed, new { value = r.RequestId })
        </td>
    </tr>
}

Or something like that :)

EDIT:

In response to a comment, I realise that my code is incorrect. Here's is an updated version:

@for(int idx = 0; idx < Model.Resources.Count; idx++)
{
    <tr>
        <td>
            @Html.CheckBoxFor(model => model.License[idx].Processed, new { value = Model.Resources[idx].RequestId })
        </td>
    </tr>
}
Sign up to request clarification or add additional context in comments.

1 Comment

I get the 'expression cannot contain an assignment operator' error when using [counter++]

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.