How to send a model in jQuery $.ajax() post request to MVC controller method

前端 未结 7 1872
南方客
南方客 2020-11-27 02:47

In doing an auto-refresh using the following code, I assumed that when I do a post, the model will automatically sent to the controller:

$.ajax({
    url: \'         


        
7条回答
  •  我在风中等你
    2020-11-27 03:08

    If you need to send the FULL model to the controller, you first need the model to be available to your javascript code.

    In our app, we do this with an extension method:

    public static class JsonExtensions
    {
        public static string ToJson(this Object obj)
        {
            return new JavaScriptSerializer().Serialize(obj);
        }
    }
    

    On the view, we use it to render the model:

    
    

    You can then pass the model variable into your $.ajax call.

提交回复
热议问题