1

How do I configure Checkboxes in Asp.Net MVC Razor.

Since in the documentation we have the following configuration Materialize for checkboxes :

<p>
   <label>
     <input type = "checkbox" />
     <span> Network </span>
   </label>
</p>

And in Razor I could not perform this configuration.

<div class = "input-field col s12">
        @Html.EditorFor (model => model.AnnualDestaque)
        @Html.LabelFor (model => model.AnnualDestaque)
        @Html.ValidationMessageFor (model => model.AnnualDestaque, "", new {@class = "text-danger"})
</div>
1
  • You need to modify the css to use a general sibling combinator (~) - refer Styling @Html.CheckBoxFor as a slider for a similar issue (materialize.css uses an adjacent sibling combinator but that will not work because your EditorFor() is generating hidden input Commented Sep 25, 2018 at 7:57

4 Answers 4

1

Please include model Name in your cshtml page

@model WebApplication3.Models.Test
@{
 ViewBag.Title = "Home Page";
 }


@using (Html.BeginForm("Save", "Home", FormMethod.Post))
 {
  <div class="row">
  <div class="col-md-4">
    @Html.TextBoxFor(m => m.EmployeeName)
    @Html.CheckBoxFor(m=>m.IsSelected)

  </div>

   <input type="submit" value="Save" />

  </div>
}

Model

public class Test
{
    public string EmployeeName { get; set; }
    public bool IsSelected { get; set; }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Even though this way not described above, the checkbox does not appear on the screen, noting that I'm using MaterializeCss, the checkbox does not appear on the screen
1

You can make it work doing this:

<label>
<input type="checkbox" name="FIELDNAME" id="FIELDNAME" value="true" class="filled-in" @(Model.FIELDNAME? "checked" : "") />
<span>@Html.DisplayNameFor(model => model.FIELDNAME)</span>
</label>
<input name="FIELDNAME" type="hidden" value="false" />

Comments

0

If you just want to have a checkbox binded with your model like that :

public class Network
{
    public bool Selected { get; set; }
    public string Name { get; set; }
}

Just use :

@Html.CheckBoxFor(m=>m.Selected)
@Html.LabelFor(m=>m.Name)

2 Comments

Even though this way not described above, the checkbox does not appear on the screen, noting that I'm using MaterializeCss, the checkbox does not appear on the screen
Unfortunately you didn't mentioned the fact that you are using a custom plugin for checkboxes. In this cases the proper way is to implement a custom helper.
0

I was able to solve it, and I am passing the answer.

I used Html Helper Documentation Html Helper MVC

It worked perfectly without mistakes the way it's meant to be.

<div class="input-field col s12">
    <label>
        <input data-val="true"
                data-val-required="The Advertisement field is required."
                id="Advertisement"/**** m => m.Advertisement ****/
                name="Advertisement"/**** m => m.Advertisement ****/
                type="checkbox"
                value="true" />

        <span>Anuncio Destaque</span>

        <input name="Advertisement" type="hidden" value="false" />
    </label>
</div>

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.