取值bool并显示为复选框

时间:2013-07-17 13:02:53

标签: asp.net-mvc razor checkbox

“供应商”表中的每条记录都有一个bool“SupplierEmailProvided”列。要在屏幕上显示值:

@Model.Suppliers.Where(s => s.Id == products.PreferredSupplierId).FirstOrDefault().SupplierEmailProvided

这样可以正常工作,但ATM会根据其值显示“真实”或“假”(在屏幕上)。

我想将其更改为复选框,如果值为FALSE则会被禁用

尝试过类似:@Html.CheckBoxFor(s => s.Suppliers.Where(x => x.SupplierEmailProvided)).FirstOrDefault().SupplierEmailProvided'

的内容

但不好。任何想法的家伙?

修改 所以我之前已经提出了所有建议。

@if((s => s.Id == products.PreferredSupplierId).FirstOrDefault().SupplierEmailProvided)
                     {   
                            @Html.CheckBoxFor(s => s.Suppliers
                                                    .Where(x => x.Id == products.PreferredSupplierId)
                                                    .FirstOrDefault()
                                                    .SupplierEmailProvided)

                             @Html.CheckBox("SupplierEmailProvided",true,
                             (Model.Suppliers.Where(x => x.SupplierEmailProvided)).FirstOrDefault().SupplierEmailProvided 
                             ? (object) new { disabled = "false" } : (object) new {disabled = "true" })
                     }

如果声明说“运营商”,那就是开始。不适用于lamba型的opperad?

任何帮助?

3 个答案:

答案 0 :(得分:2)

假设SupplierEmailProvided是一个布尔类型,虽然逻辑应该是明确的,但尚未测试

如前所述,当值为false时,复选框应处于禁用状态。

@if(Model.Suppliers.Where(x => x.SupplierEmailProvided)).FirstOrDefault().SupplierEmailProvided)
{
     Html.CheckBox("SupplierEmailProvided",true)
}else
{
     Html.CheckBox("SupplierEmailProvided",false,new{ disabled ="true"} )
}

答案 1 :(得分:0)

你试过这个吗?

@if(Model.Suppliers.FirstOrDefault(s => s.Id == products.PreferredSupplierId).SupplierEmailProvided)
{
    html code 
}
else 
{
    ...
}

答案 2 :(得分:0)

CheckBoxFor应该有效,请检查括号,将分配分成两半:

@{
    var supplier = Model.Suppliers.Where(s => s.Id == products.PreferredSupplierId).FirstOrDefault();
}
@Html.CheckBoxFor(model => supplier.SupplierEmailProvided)

或者你在哪一行:

@Html.CheckBoxFor(model => model.Suppliers
                                .Where(s => s.Id == products.PreferredSupplierId)
                                .FirstOrDefault()
                                .SupplierEmailProvided)