Click here to Skip to main content
15,886,077 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to Enable/Disable a html control in MVC 4, please check with the below code

My View Page is
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" id="btnAddComp" value="Reset"
@{ if (ViewBag.CompAddFlg != true)
{ @:disabled="disabled"
}
}
onclick="clearTextBox();">
Add New Company

My Controller is
if (list.AddFlg == 1)
{
ViewBag.CompAddFlg = "true";
}
else
{
ViewBag.CompAddFlg = "false";
}

What I have tried:

Dear Comrades please check with my code and let me know the changes....
Posted
Updated 7-Dec-18 1:17am

1 solution

"true" - this is a string
true - this is a boolean

Those two are different types, you can't compare them. Use bools everywhere

if (list.AddFlg == 1)
{
    ViewBag.CompAddFlg = true;
}
else
{
    ViewBag.CompAddFlg = false;
}
 
Share this answer
 
v2
Comments
peer mohammed.A 7-Dec-18 7:28am    
Yes i tried,but it not enable the button...
peer mohammed.A 7-Dec-18 7:33am    
Ill share my Controller and View below

1.My method in Controller is
if (list.AddFlg == 1)
{
ViewBag.CompAddFlg = true;
}
else
{
ViewBag.CompAddFlg = false;
}
2.My View is



button type="button" class="btn btn-primary" data-toggle="modal" data-target="#myModal" id="btnAddComp" value="Reset"
@{ if (ViewBag.CompAddFlg != true)
{ @:disabled="disabled"
}
}
onclick="clearTextBox();">
Add New Company


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