0

Please find the code which i tried to update the DB using MVC2.But unable to update

View Page with Ajax Code

<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="../../Scripts/MicrosoftAjax.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
   $('#cbID').click(function(){
    $.ajax({ url: 'Home/About',
    type: 'POST',
    data: { checkbox: $('#cbID').attr('checked') },
    success: function(o) { alert('saved'); }
                      });
</script>
<div class="bgDiv">
<input id="cbID" type="checkbox" name="SelectedObject" value="cbValue" />

Controller page code

 public ActionResult About(string str)
        {
            AboutModels ObjAM = new AboutModels();//model class name
            polloptions = ObjAM.dbValue(str);//call the model function to udate the table
            return View();
        }

Please advice

1 Answer 1

1

you should either declare your event handler in ready function or declare it with live or delegate methods like

<script language="javascript" type="text/javascript">
$(function(){   
  $('#cbID').click(function(){
    $.ajax({ url: 'Home/About',
    type: 'POST',
    data: { checkbox: $('#cbID').attr('checked') },
    success: function(o) { alert('saved'); }
                      });
      });
 });
</script>

the problem is that your script is running before the required checkbox is rendered so putting it in ready will wait until document is ready or live will bind it on document level where the event will reach through propogation

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

10 Comments

Sorry i am not cleared can you please elaborate your explanation
Please try the code first and also tell me what specific point you want me to explain
I have updated my <script> and run it. But i didn get the output what i expect.
If my understanding correct - alert box will come after i select the check box rite?
we had mismatching braces and parenthesis in code. corrected the code. plz give it a try and do consider using getfirebug.com for js debugging
|

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.