Click here to Skip to main content
15,900,664 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have created a checkbox using textbox in asp.net mvc 4 on view page like this.

@Html.TextBox("SomeID", "SomeValue" ,new { type = "Checkbox" })

now based on some condition, I would like to check this checkbox.

How could it be possible?

I have tried @Html.TextBox("SomeID", "SomeValue" ,new { type = "Checkbox", value = "true" })

but it is not working.

Thank you.
Posted

You should set the AllowRating property to true, preferably in the controller or model.
Like other inputs, the checkbox's state reflects the value of the property.
 
Share this answer
 
by using javaScript/jquery you can Check/Uncheck the checkBox

JavaScript
<script type="text/javascript">
    if (//Here Check your Condition)
    {
        $("#SomeID").attr('checked', true);
    }

</script>


OR

HTML
@Html.TextBox("SomeID", "someValue" ,new { type = "Checkbox",@checked=true })
 
Share this answer
 
v3
set this
ViewBag.Checkboxvale
in previous action where it comes from

var ischeck= ='@ViewBag.Checkboxvalue';

if(ischeck=="Y")
{
$("#SomeID").attr('checked', true);

}
else{
$("#SomeID").removeAttr('checked', true);

}
 
Share this answer
 
Why not to use @Html.Checkbox()?
为何不直接用@Html.Checkbox()
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900