0

In my class i have more attributes and all have the good value when i check after submit the form All variables are fine except the bool who is always false. Can someone help me? My class:

public class Pilot
{
    .....
    public bool OwnerOrPart { get; set; }
    .....
}

My model:

public class IardModel : RepositoryCollection
{
    .......
    public List<Pilot> Pilot { get; set; }
    .....
    public IardModel()
    {
        .....
        Pilot = new List<Pilot>();
        .....
    }
}

Controller: public ActionResult Aeronef(IardModel model, string id) { if (id != "") { using (var scope = RepositoryContainer.Container.BeginLifetimeScope()) { var repository = scope.Resolve(); model = repository.SingleById(id); model.IsEditing = true; return View(model); } } model.IsEditing = false; return View(model); } And the view:

@if (Model.Pilot.Count == 0)
{
    ....
    <section class="col col-md-2">
        <label class="label-form">(Co)Propriétaire</label>
        <label class="checkbox">
            <input name="Pilot[0].OwnerOrPart" type="checkbox" class="form-control">
            <i></i>
        </label>
    </section>
}
@foreach (var pilots in Model.Pilot)
{
    ....
    <section class="col col-md-2">
        <label class="label-form">(Co)Propriétaire</label>
        <label class="checkbox">
            <input name="Pilot[@(Model.Pilot.IndexOf(pilots))].OwnerOrPart" type="checkbox" class="form-control" @(pilots.OwnerOrPart ? "checked='checked'" : "")>
             <i></i>
         </label>
    </section>
}

All variables are fine except the bool who is always false. Can someone help me

florea_g
  • 1
  • 3
  • My controller: `public ActionResult Aeronef(IardModel model, string id) { if (id != "") { using (var scope = RepositoryContainer.Container.BeginLifetimeScope()) { var repository = scope.Resolve(); model = repository.SingleById(id); model.IsEditing = true; return View(model); } } model.IsEditing = false; return View(model); }` – florea_g Oct 27 '15 at 12:46

2 Answers2

0

You can use @Html.CheckBoxFor(m => m.Pilot[0].OwnerOrPart, new {@class = "form-control"}) instead of <input type="checkbox" />

Edit

I have write the code corresponding to this,

@if (Model.Pilot.Count == 0)
{
    ....
    <section class="col col-md-2">
        <label class="label-form">(Co)Propriétaire</label>
        <label class="checkbox">
            @Html.CheckBoxFor(m => m.Pilot[0].OwnerOrPart, new {@class = "form-control"})
            <i></i>
        </label>
    </section>
}

You can simply write this as,

@for(int i=0; i < Model.Pilot.Count; i++){
    ....
    <section class="col col-md-2">
        <label class="label-form">(Co)Propriétaire</label>
        <label class="checkbox">
        @Html.CheckBoxFor(m => m.Pilot[i].OwnerOrPart, new {@class = "form-control"})
        <i></i>
        </label>
    </section>
}
Abhilash Augustine
  • 4,128
  • 1
  • 24
  • 24
0

You should better use: <input ... @(pilots.OwnerOrPart ? "checked='true'" : "")>

See this
How do I set a checkbox in razor view?
and this
Proper usage of .net MVC Html.CheckBoxFor

Community
  • 1
  • 1
Fayyaz Naqvi
  • 695
  • 7
  • 19
  • The foreach is for edit the view so my problem is in if. My value in not binded is allways false. Maybe my checked is wrong but it will never work because my value isn't good – florea_g Oct 27 '15 at 11:28