2

i have a checkbox that i'm using the model's company value to see if it should be checked. company value can have more than one company as such: 1,9,10,15

in this case of multiple companies, following statement will never be true.

<input type=checkbox id=drsc class="comp" value="9" data-mini="true"   @(Model.company=="9" ? "data-chkd=true" : "") />

so i'm trying to use logic below using the 'contains' clause and it doesn't seem to be working.

<input type=checkbox id=nn class="comp" value="9" data-mini="true"  @((Model.company).Contains("9") ? "data-chkd=true" : "") />

if my company field has just company 9, first stmt is working. but not 2nd. any thoughts?

5
  • 1
    You should really show your code. Commented Sep 13, 2013 at 20:29
  • @Ek0nomik found it: he put it up, but not formatted correctly, so it was invisible Commented Sep 13, 2013 at 20:33
  • 1
    What type is Model.company? It looks like a string, but consider the fact that "10".Contains("1") is true. That looks like it should be an IList<int>. Commented Sep 13, 2013 at 20:36
  • Is the company property just a comma delimited string? Should it be a list instead? update: heh, posted at same time as Tim. Commented Sep 13, 2013 at 20:36
  • post the string that is Model.company Commented Sep 13, 2013 at 20:51

1 Answer 1

3

UPD:

Try use razor syntax:

@Html.CheckBox("checkBoxName", Model.company == "9", new { id = "drsc", @class = "comp", value = "9", data_mini = "true" })

Result html code will be:

<input class="comp" data-mini="true" id="drsc" name="checkBoxName" type="checkbox" value="9">

if you have array of companies like this:

public class TmpModel
{
    public IEnumerable<int> companies = new int[] { 1, 9, 10, 15 };
}

this code make checkbox checked if company 9 is one of model.companies

@Html.CheckBox("checkBoxName", Model.companies.Contains(9), new { id = "drsc", @class = "comp", value = "9", data_mini = "true" })
Sign up to request clarification or add additional context in comments.

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.