質問

In my view I have,

@Html.CheckBoxFor(m => m.IsExist, new { @id = "IsExist" })

In my Model I have IsExist value from DB. Either true or false.

Now how can I 'Check' or 'Uncheck' the option, based on the true or false value in IsExist. I expected, on binding by default the check box takes the value of Model. Howeverr that is not happening.

How can i achieve it?

役に立ちましたか?

解決 2

You can do this to check a checkbox :

if (Model.IsExist) {
    @Html.CheckBoxFor(m => m.IsExist, new { @id = "IsExist", "checked" = "checked"}) 
} 

Hope it's help

他のヒント

Here how i achieved it

@Html.CheckBox("VC",(item.isVC=="Y") ? true : false)

where my item.isVC has value "Y" or "N"

Below snippet indicates an alternate way of having checkbox created and conditionally making it checked/unchecked. The following approach is also useful to get the value of checkbox by traditional approach using jquery.

@model myproject.Models.StudentModel


<div class="row">
   <label class="checkbox-inline">
     <input type="checkbox" name="selfStudy" checked="@Model.IsExist"><b>Self Study</b>
   </label>
</div>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top