3

C# model class

public class SubCategoryTwoViewModel
{
    public long Id { get; set; }
    public string SubCatTwoName { get; set; }
    public CategoryViewModel Category { get; set; }
    public SubCategoryOneViewModel SubCatOne { get; set; }
    public string PictureUrl { get; set; }
    public List<IFormFile> File { get; set; }
    public UserViewModel AddedBy { get; set; }
    public DateTime AddedOn { get; set; }
    public UserViewModel Updated_By { get; set; }
    public DateTime? UpdatedOn { get; set; }
    public bool Active { get; set; }
}

Here is the CategoryViewModel

public class CategoryViewModel
    {
        public long CategoryId { get; set; }
}

Here is the controller method

[HttpPost]
public JsonResult AddEditSubCategoryTwo(SubCategoryTwoViewModel model)
{
}

Here is the ajax call which use the Form Data to serialized the data send to the controller method.

var ajaxUrl = ApplicationRootUrl("AddEditSubCategoryTwo", "Category");
var formData = new FormData();
var totalFiles = document.getElementById("subCatFile").files.length;

for (var i = 0; i < totalFiles; i++) {
    var file = document.getElementById("subCatFile").files[i];
    formData.append("File", file);
}

formData.append('SubCatTwoName', self.subCatTwoName());
var category = {
    CategoryId: self.selectCategory()
};

formData.append('Category', category);
var subCatOne= {
    SubCategoryOneId: self.selectCategorytwo()
};

formData.append('SubCatOne', subCatOne);
formData.append('Active', self.active());

$.ajax({
    type: "POST",
    contentType: false,
    processData: false,
    url: ajaxUrl,
    data: formData,
    dataType: "json",
    success: function (data) {
    }
});

I,am getting some field data in the controller but the java script value is not serialized to the controller model.

Here is the screenshot of the sample Data of some field

In the screen shot I am getting the Category & SubCatOne field as null. But active, SubCatTwoName and File field has receive the value. I want to get Category & SubCatOne value in the controller model object. How can I achieved this.

10
  • You are appending a javascript object to FormData keys "SubCatTwoName" and "Category". What are you expecting at server for keys "SubCatTwoName" and "Category"? Commented Apr 21, 2017 at 4:16
  • I have received the SubCatTwoName value in server side . However I, have not received the value for Category. Commented Apr 21, 2017 at 4:20
  • What does console.log(category) log? Commented Apr 21, 2017 at 4:22
  • Object {CategoryId: 5} This is the console output Commented Apr 21, 2017 at 4:27
  • Have not tried asp.net-core. Have you tried passing category to JSON.stringify()? Commented Apr 21, 2017 at 4:36

1 Answer 1

3

Trying to add the object directly will not work. Instead you can make use of model binding by adding the nested fields with the correct name (separate the property hierarchy with dots). e.g.

formData.append('Category.CategoryId', self.selectCategory());
Sign up to request clarification or add additional context in comments.

1 Comment

@Paul Hiles, does that mean we can't send json object in formData and handle it properly via model in asp.net core action?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.