开发者

How to call server-side method within client-side method

I have an html menu, when one item from it gets clicked I want to call a javascript handler and within it I want to call a server side method, how can I do that?

I'll talk in details: I have a file menu, when user click "create directory" -> a java script handler works to add that directory (as a node) to a tree view. At the end of the javascript handler I want to call an asp.net method that add that directory to the database.

EDIT 2: I've tried to use JQuery.ajax() to access the server-side method within the client-side event handler ..this is what my code looks like:

Controller

   [HttpPost]
        public ActionResult addDirectory(string directoryName)
        {
            Directory dir = new Directory();
          1-  dir.dateCreated = DateTime.Now;
          2-  dir.dateAccessed = DateTime.Now;
          3-  dir.dateModified = DateTime.Now;
          4- dir.ImgURL = "~/Images/Folder-Add-icon.png";
          5-  dir.DirectoryName = directoryName;
          6-  dirRepo开发者_C百科.addDirectory(dir);
          7- dirRepo.Save();
            return new JsonResult { Data = dir };

    }

View

 $.ajax({
            url: "Explorer/addDirectory", 
            type: "POST",
            dataType: "json",
            data: {param1: name},

          complete: function() {
              alert("finished");
          },

          success: function(data) {
          alert("added");
         },

          error: function() {
          alert("error");
          },
        });

The problem is, the "error" method gets called always if I add 1-7 lines in the controller method .. if I comment them out the success method will get called, WHY?


controller code

[HttpPost]
public ActionResult SomeMethod(int param1, string param2) {
    return Json(someobject);
}

javascript with jQuery

 $.ajax({
    url: "controller/somemethod", // or <%=Url helper method can't remember
    type: "POST",
    dataType: "json",
    data: {param1: 10, param2: "x"},

  complete: function() {
      //called when complete
  },

  success: function(data) {
      // json returned from server
      //called when successful
 },

  error: function() {
      //called when there is an error
  },
});

I suggest googling online for mvc tutorials


To call a server side method on a client side event you need to do the following:

1- Create the server side method:

void DoSomething(...) { ... }

2- Implement RaisePostBackEvent of the System.Web.UI.IPostBackEventHandler interface which take one string argument (You can use it somehow ..).:

public void RaisePostBackEvent(string eventArgument) {
  if (eventArgument == "value") DoSomething(...);
}

3- Write a script to trigger post back:

function TriggerPostBack(control, arg){ __doPostBack(control, arg); } 

4- Call the PostBack trigger function when needed:

<asp:Button.... onclick="TriggerPostBack('button', 'btnOk')" .. /> 


Create a web method

[WebMethod]
static string FooBar(string arg) {

}

Call WebMethod with ajax. $.ajax or XMLHttpRequest

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜