How to pass model in ajax post request?

前端 未结 1 496
野趣味
野趣味 2021-02-10 07:44

everybody. I\'m new in asp mvc. I need to pass my model as parameter in ajax post request.

Here is my ajax post request code:



        
1条回答
  •  栀梦
    栀梦 (楼主)
    2021-02-10 08:23

    Please have a look on article: http://www.codeproject.com/Articles/678591/CRUD-Operations-using-Partial-V

    In this article, CRUD operations are performed using jQuery AJAX calls in ASP.NET MVC 4 Application.

    About your code, you need to modify below line:

                $("#contragentTable tr").click(function () {
                    var modelDataJSON = '@Html.Raw(Json.Encode(Model))';
    
                    $.ajax({
                    url: "/Contragent/Index",
                    type: 'POST',                   
                    data: { myObject1: modelDataJSON},                              
                    dataType: 'json'                    
                    });
                 });                   
    

    You must provide a name to object in AJAX call and it should same as the argument name in targeted controller action method and Since you are sending JSON from client so action method should be like:

    public ActionResult Index(string myObject1 ) 
    

    Then inside action you can deserialize the JSON object and create model or whatever processing you need.

    0 讨论(0)
提交回复
热议问题