2

I am trying to implement material css to my mvc app, but checkboxfor is giving me hard time. The thing is check box in materialcss has a specific usage setup like this

@for (int j = 0; j < Model.Questions[i].Answers.Count; j++)
{
    <label>
        <input type="checkbox" />
        <span>Red</span>
     </label>
}

if you don't do it like this it won't work, so, naturally I've tried

@for (int j = 0; j < Model.Questions[i].Answers.Count; j++)
    {
    <label>
        @Html.EditorFor(model => Model.Questions[i].Answers[j].IsTrueUserChoice)
        @* or this *@
        @Html.CheckBoxFor(model => Model.Questions[i].Answers[j].IsTrueUserChoice)
        <span></span>
    </label>
}

htm.editorfor and checkboxfor render 2 checkbox tags one is visible and one is hidden (that how checkbox work in mvc), but with materializecss I can't do that because <span></span> has to go bellow <input type="checkbox"/> so this is not going to work because the hidden checkbox field is below checkbox.

Do you have any suggestions on how to fix this?

7
  • Looks like whoever implemented checkboxes got a little bit carried away. I don't see how this is possible to achieve. I would file a issue. Title it something like "Razor rendering engine can't render materialize.css checkboxes". PS: also, for completeness, please include the outer for loop with i variable in it in the code snippet. Commented Jul 23, 2018 at 19:54
  • 1
    thanks for comment. filing an issue would take too long, probably wouldnt be fixed. i am sure this can be done with custom html helper Commented Jul 23, 2018 at 20:35
  • Good point. I completely forgot about those. Commented Jul 23, 2018 at 20:45
  • You need to modify the css to us a sibling selectior. Refer this answer for an example Commented Jul 23, 2018 at 22:49
  • @StephenMuecke no luck with that Commented Jul 24, 2018 at 15:58

1 Answer 1

0

Use "DisplayNameFor"...

<label>
    @Html.CheckBoxFor(model => Model.Questions[i].Answers[j].IsTrueUserChoice)
    <span>@Html.DisplayNameFor(model => Model.Questions[i].Answers[j].IsTrueUserChoice)</span>
</label>

And modify the relevant CSS:

label input[type=checkbox].checkbox ~ span:before 
{
    ....
}
Sign up to request clarification or add additional context in comments.

1 Comment

i fixed it with custom html helper

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.