Home > OS >  List of Objects and Id not getting passed to MVC Controller Method Using jQuery Ajax
List of Objects and Id not getting passed to MVC Controller Method Using jQuery Ajax

Time:01-28

Im trying to pass an Id and object of picList to the controller and it shows up null. I've looked at all the other SO solutions and changed my code to what they said and Im still getting null for both values in the controller.

So I've even tried to change the data that is being sent to the controller as such to see if that made any difference and it didn't.

in ajax call i changed the data to such

 data: {"Name": "Adam"},

and added this to the controller and still nothing is getting passed.

UnitImages(string Name,..

here is what the JSON.stringify(data) looks like.

enter image description here

View Model

 public class FileViewModel
 {
    public FileViewModel()
    {
        UnitPicturesList = new List<UnitPictures>();
    }

    public IList<IFormFile> Files { get; set; }
    [Required]
    public int AuctionId { get; set; }
    public string FileLocation { get; set; }
    public List<UnitPictures> UnitPicturesList { get; set; }
 }

model

public class UnitPictures
{
    public long ImageId { get; set; }
    public string FileName { get; set; }
    public string FileLocation { get; set; }
    public int SortOrder { get; set; }
}

controller

[HttpPost]
 public ActionResult UnitImages(long auctionId, List<UnitPictures> picList)
 { ...
 }

Ajax call

    function UpdateImages(auctionId, picList) {

    var data = { auctionId: auctionId, picList: picList };

    console.log(JSON.stringify(data));
     
    $.ajax({
           cache: false, 
           contentType: "application/json; charset=utf-8",
           dataType: "json", 
           type: "POST", 
           url: '/PhotoUploader/UnitImages',
           data: JSON.stringify(data), 
           success: function(data){ 
               if(data.Result == 1) { 
                   alert("images where successfully updated.");
               }else { 
                   alert('images where successfully updated.'); 
               }
           }, 
           error: function() { 
               alert("The images were not updated because of a problem.")
           }
   });

}

CodePudding user response:

Try using a class that match the posted model. Something like this:

public class UnitPictures_ViewModel
{
    public int AuctionId {get;set;}
    public List<UnitPictures> PicList { get; set; }
    
}

public class UnitPictures
{
    public long ImageId { get; set; }
    public string FileName { get; set; }
    public string FileLocation { get; set; }
    public int SortOrder { get; set; }
}

CodePudding user response:

Asp.net core MVC default binding value from form, Here you can try to add [FromBody] attribute on your parameter to change the resource to bind value from body.

[HttpPost]
 public ActionResult UnitImages([FromBody]string Name)
 { ...
 }

Model details you can refer to Model Binding.

  • Related