0

Is it possible to pass the form data with out using FormData by using the JavaScript Model. As my form has many controls I would like to go with Model approach instead of FormData.

Is there a way to pass selected files to controller with out using HttpPostedFileBase? I have nearly 10 different class which accepts HttpPostedFileBase

I have a class as follows in c#

public class ArtGallery{
   Public string GallerName {get;set;}
   Public HttpPostedFileBase[] Documents {get;set;}
}

The equivalent JavaScript Model is

class ArtGallery{
     GallerName;
     Documents[];
}

On my save of the form here is how I am doing it

function saveFormData(){
  let gallery = new ArtGallery();
  gallery.GallerName = "Test";
  $.each($("input[type=file]"), function (i, obj) {
        $.each(obj.files, function (j, file) {
            gallery.Documents.push(file);
        });
   });
   saveToDb();
}

function saveToDb() {
    let url = "/MyController/PostData/";
    $.ajax({
        url: url,
        type: 'POST',
        async: false,
        data: '{gallery : ' + JSON.stringify(gallery) + '}',
        contentType: 'application/json; charset=utf-8',
        dataType: 'json',
        success: function (result) {
        },
        error: function (request) {
        }
    });
}

My controller is as follows, which is getting triggered but documents I am unable to get

[HttpPost]
public JsonResult PostUserData(ArtGallery gallery) {

}
3
  • How is your Document Model looks like? what you see when you write JSON.stringify(console.gallery.Documents) Commented Oct 8, 2021 at 9:03
  • @Developer It is showing empty :( Commented Oct 8, 2021 at 9:10
  • then there is some issue with stringify of documents Commented Oct 8, 2021 at 14:08

0

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.