2017-05-04 2 views
-1

Ich bin nicht sicher, warum der Controller Daten von einem Ajax-Aufruf empfängt. Kann ich etwas falsch machen?Controller in meinem MVC-Projekt Null von Ajax Post-Anruf

[HttpPost] 
     [Route("Product/UpdateDetails")] 
     public ActionResult UpdateProduct (ProductModel model) <<// model here is null 
     { 
      Product p = new Product 
      { 
       ProductId = p.ProductId, 
       Price = p.Price, 

      }; 


      return View("_ProductDetail"); } 

Ajax-Aufruf unter:

var model = { 
      ProductId: 1, 
      Price: 270.99, 

     }; 

     var json = JSON.stringify(model) 


     $.ajax({ 
      url: '/Product/UpdateDetails', 
      type: 'Post', 
      contentType: "application/json; charset=utf-8", 
      model: model, 
      success: function (results) { 

      } 
     }); 


//Model 
public class Product 
{ 
public int Id {get;set;} 
public double Price {get;set;} 
} 

Könnt ihr etwas erkennen, dass ich falsch oben im Code tun kann? Ich kann nichts sehen, was ich falsch mache.

+0

Löschen Sie einfach 'contentType:" application/json; charset = utf-8 ",' und 'model: model,' und fügen Sie 'data: model' hinzu. (Es ist nicht notwendig, die Daten zu stringieren, aber Sie haben es trotzdem nicht benutzt) –

Antwort

1

Try this:

$.ajax({ 
    url: '/Product/UpdateDetails', 
    type: 'Post', 
    contentType: "application/json; charset=utf-8", 
    data: json, 
    success: function (results) { 

    } 
}); 

Sie verwenden JSON.Stringify() auf Ihrem Modell, aber vergessen, die Variable "json" auf dem Ajax-Aufruf zu verwenden, so dass das Ajax versucht, einen "nicht-json" -Modell zu posten.

Auch gibt es keine model Einstellung in Ajax Anrufe, die richtige, um Ihre Daten zu posten ist data, wie Sie here sehen können.

+0

Vielen Dank! – 1future

Verwandte Themen