【问题标题】:Razor checkbox from list always false列表中的 Razor 复选框始终为 false
【发布时间】:2015-10-27 11:06:12
【问题描述】:

在我的班级中,我有更多的属性,并且在我提交表单后检查时都具有良好的价值 除了总是为假的布尔值之外,所有变量都很好。 有人能帮我吗? 我的班级:

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

我的模特:

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

控制器: 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); } 以及观点:

@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>
}

所有变量都很好,除了总是为假的布尔值。 谁能帮帮我

【问题讨论】:

  • 我的控制器:public ActionResult Aeronef(IardModel model, string id) { if (id != "") { using (var scope = RepositoryContainer.Container.BeginLifetimeScope()) { var repository = scope.Resolve&lt;IRepository&gt;(); model = repository.SingleById&lt;IardModel&gt;(id); model.IsEditing = true; return View(model); } } model.IsEditing = false; return View(model); }

标签: c# asp.net-mvc razor


【解决方案1】:

你最好使用:&lt;input ... @(pilots.OwnerOrPart ? "checked='true'" : "")&gt;

看到这个
How do I set a checkbox in razor view?
还有这个
Proper usage of .net MVC Html.CheckBoxFor

【讨论】:

  • foreach 用于编辑视图,所以我的问题在于 if。我在未绑定中的价值总是错误的。也许我的检查是错误的,但它永远不会起作用,因为我的价值不好
【解决方案2】:

您可以使用@Html.CheckBoxFor(m =&gt; m.Pilot[0].OwnerOrPart, new {@class = "form-control"}) 代替&lt;input type="checkbox" /&gt;

编辑

我已经写了对应的代码,

@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>
}

你可以简单地写成,

@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>
}

【讨论】:

  • 索引超出范围。必须是非负数且小于集合的大小。参数名称:索引
  • @florea_g,对不起,我只是写你提供的代码。
  • @florea_g,请查看更新后的答案。
  • @florea_g,很抱歉这个问题。是的,我从你的问题中得到了它。
  • @florea_g,我已经在我的机器中使用测试数据对其进行了测试。它工作正常。你能检查一下你的数据吗?
猜你喜欢
  • 2015-03-16
  • 1970-01-01
  • 2020-09-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多