Before i start i am not sure if i searched "correctly" i have tried multiple things on what i will explain and none of them works at the end because i have to use html helpers.
what i am trying to do is using ccs3 create custom checkbox. i found multiple designs and i just followed them but as i use Html.CheckBoxFor(..., htmlAttributes: new { id = "A Number"})
. "A Number" here is just a generated number that goes threw a loop, just so i don't do things manually.
this is how i handle the custom checkboxes (1)
<p>
<input type="checkbox" value="..." id="@count" />
<label for="@count"><span class="ui"></span>Name</label>
</p>
when i use html helper checkboxfor and when i use it this way it wont work i am not sure why or i am just confused about it. (2)
<p>
@Html.CheckBoxFor(m => m..., htmlAttributes: new { id = count })
<label for="@count"><span class="ui"></span>...</label>
</p>
now when i tried (2) the first time it didn't work and i thought it was from me but when i tried (1) it worked wonders expect that when i submit it doesn't return the value of the checkbox. when i searched i found this Addind css classes to razor elements the response from darin-dimitrov was interesting but i use a custom template calling it simply by Layout = "..."
in my view. this part of the answer is flou for me i am not sure how i would declare this nor how i called and if it's unique or changes all.
@Html.TextBox(
"",
ViewData.TemplateInfo.FormattedModelValue,
ViewData
)
css3 code
/*
Custom Checkboxes
*/
[type="checkbox"]:not(:checked),
input[type="checkbox"]:checked {
position: absolute;
left: -9999px;
}
[type="checkbox"]:not(:checked) + label,
[type="checkbox"]:checked + label {
position: relative;
padding-left: 75px;
cursor: pointer;
}
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before,
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
content: '';
position: absolute;
}
[type="checkbox"]:not(:checked) + label:before,
[type="checkbox"]:checked + label:before {
left:0; top: -3px;
width: 65px; height: 30px;
background: #DDDDDD;
border-radius: 15px;
-webkit-transition: background-color .2s;
-moz-transition: background-color .2s;
-ms-transition: background-color .2s;
transition: background-color .2s;
}
[type="checkbox"]:not(:checked) + label:after,
[type="checkbox"]:checked + label:after {
width: 20px; height: 20px;
-webkit-transition: all .2s;
-moz-transition: all .2s;
-ms-transition: all .2s;
transition: all .2s;
border-radius: 50%;
background: #7F8C9A;
top: 2px; left: 5px;
}
/* on checked */
[type="checkbox"]:checked + label:before {
background:#34495E;
}
[type="checkbox"]:checked + label:after {
background: #39D2B4;
top: 2px; left: 40px;
}
[type="checkbox"]:checked + label .ui,
[type="checkbox"]:not(:checked) + label .ui:before,
[type="checkbox"]:checked + label .ui:after {
position: absolute;
left: 6px;
width: 65px;
border-radius: 15px;
font-size: 16px;
font-weight: bold;
line-height: 22px;
-webkit-transition: all .2s;
-moz-transition: all .2s;
-ms-transition: all .2s;
transition: all .2s;
}
[type="checkbox"]:not(:checked) + label .ui:before {
content: "non";
left: 32px
}
[type="checkbox"]:checked + label .ui:after {
content: "oui";
color: #39D2B4;
}
[type="checkbox"]:focus + label:before {
border: 1px dashed #777;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
-ms-box-sizing: border-box;
box-sizing: border-box;
margin-top: -1px;
UPDATE
The View
I can provide an explanation if needed of the view
@model WebAppWithEF.Models.MainModel
@using WebAppWithEF.Models;
@{
ViewBag.Title = "Page Title";
Layout = "~/Views/Shared/_TemplateRest.cshtml";
}
@using (Html.BeginForm(null, "Export", new { entity_name = Model.which_entity_to_show }, FormMethod.Post, null))
{
<div>
<span class="text-uppercase">Nom Définition </span>
@Html.EditorFor(m => m.exportDefinition.name, new { htmlAttributes = new { maxlength = "50" } }) <br />
@if (Model.is_any_checked == false)
{
@Html.Label("Error MSG", htmlAttributes: new { @class = "text-danger" }) <br/>
}
<br />
@for (int count = 0; count < Model.selectedFields.Count(); count++)
{
<p>
@Html.CustomCheckBoxFor(m => m.selectedFields[count].propertyStatus)
@Html.CustomLabelFor(m => m.selectedFields[count].propertyStatus, Model.selectedFields[count].propertyName, "<span class=\"ui\"></span>", new { @class = "class" })
</p>
@Html.HiddenFor(m => m.selectedFields[count].propertyStatus)
@Html.HiddenFor(m => m.selectedFields[count].propertyName)
}
@Html.HiddenFor(m => m.exportDefinition.pkey)
@Html.HiddenFor(m => m.exportDefinition.entityID)
<div class="col-md-offset-0 col-md-10">
<div class="contact-button">
<button name="SaveDefinition" onclick="submit()">Enregistrer/Modifier</button>
</div>
</div>
<div class="col-md-offset-0 col-md-10">
<div class="contact-button">
<button name="SpecificExport" onclick="submit()">Exporter</button>
</div>
</div>
@if (!Model.is_new)
{
<div class="col-md-offset-0 col-md-10">
<div class="contact-button">
<button name="DeleteDefinition" onclick="submit()">Supprimer</button>
</div>
</div>
}
<div class="col-md-offset-0 col-md-10">
<div class="contact-button">
<button onclick="@Url.Action("Index", "Home" , new { entity_name = ViewBag.entity_name })">Retour</button>
</div>
</div>
</div>
}