0

The following piece of code is not disabling the html button. I am using MVC3 razor . Can anyone point out where I am going wrong

@{
   bool disablebutton = bool.Parse(ViewData["Disablebutton"].ToString());        
 }    

 $(document).ready(function () {  
   if (@disablebutton) {
     $('#abc').attr('disabled', 'disabled');
   }       
 });
3
  • Need more context. button with id abc is in the document? What's the resulting HTML look like (i.e. view source in browser and paste that, not just your razor code)? Commented Nov 8, 2012 at 21:28
  • The below code is from view source <script type="text/javascript"> $(document).ready(function () { if (True) { $('#abc').attr('disabled', 'disabled'); } }); </script> <input type="button" value="abc" id="abc" /> Commented Nov 8, 2012 at 21:38
  • Added an answer with my response Commented Nov 8, 2012 at 21:50

1 Answer 1

1

Based on your comment in your question, there's your problem: "True" is not valid Javascript. It has to be true all lowercase. Look at your JS error console and I'm sure you'll have an error.

You can do if (@disablebutton.ToLower())

The above is just one way of resolving your issue. You need to be careful when mixing server-side with client-side.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.