0

I have two user controls on the page and one of the user control has this text aread. which is used to add a note and but when they click add note button the page reloads. I do not want the page to reload ,i was looking for an example which this is done without postback.

Thanks

i tired doing this using JSON , but it throws the following error

The HTTP verb POST used to access path '/Documents/TestNote/Documents/AddNote' is not allowed.

<script type="text/javascript">
    $(document).ready(function() {

        $("#btnAddNote").click(function() {
            alert("knock knock");
            var gnote = getNotes();
            //var notes = $("#txtNote").val();
            if (gnote == null) {
                alert("Note is null");
                return;

            }

            $.post("Documents/AddNote", gnote, function(data) {
                var msg = data.Msg;
                $("#resultMsg").html(msg);
            });
        });
    });

    function getNotes() {
        alert("I am in getNotes function");
        var notes = $("#txtNote").val();
        if (notes == "")
            alert("notes is empty");
        return (notes == "") ? null : { Note: notes };
    }
</script>
</asp:Content>
2
  • Can you provide more detail? Is this button a form submit button? You mention postback too - are you really using MVC or are you actually using webforms? Commented May 3, 2010 at 20:37
  • It's some thing like this,I am using asp.net mvc <input type="submit" value="Add Note" onclick="AddNote" /> Commented May 3, 2010 at 20:46

2 Answers 2

1

Something like this would give you the ability to send data to an action do some logic and return a Json result then you can update your View accordingly.

Javascript Function

function AddNote(){
   var d = new Date(); // IE hack to prevent caching

   $.getJSON('/MyController/MyAction', { data: "hello world", Date: d.getTime() }, function(data) {
     alert(data);
     // call back update your view here
   });
}

MyController Action

public virtual JsonResult MyAction(string data)
{
   // do stuff with your data here, which right now my data equals hello world for this example
   return Json("ReturnSomeObject");
}
Sign up to request clarification or add additional context in comments.

1 Comment

please see above i added some code which i am trying to execute but it's throwing error. Thank you.
0

What you want is AJAX update. There will always be a postback (unless you are satisfied with simple Javascript page update that does not save on the server), but it won't be the flashing screen effect any more.

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.