1
$("#btn2").click(function () {
            var data = new FormData();
            var Form = $("#form2").serialize();
            var send = {FormData+&+ data};

            $.ajax({
                url: '@Url.Action("AddProduct", "Product")',
                data: send,
                cache: false,
                contentType: false,
                processData: false
            });
        });

How to send FormData and Form object through ajax, please check the code its not working but it will tell you what I want to achieve And also please mention how to receive this in Controller. Basically I want to send picture and formcollection to my controller

Thanks

Fahad Meraj
  • 109
  • 1
  • 10
  • this code will send your form data to your controller in a model , what error are you getting? – Dragon Oct 03 '15 at 21:45

2 Answers2

1
 $("#btn2").click(function () {
            var data = new FormData($('.form').get(0));

            $.ajax({
                url: '@Url.Action("AddProduct", "Product")',
                type: 'POST',
                data: data,
                cache: false,
                contentType: false,
                processData: false
            });
        });

Through googling I got this code but my controller still receiving null??

Controller

public void AddProduct(Product product, HttpPostedFileBase myImage){}

Fahad Meraj
  • 109
  • 1
  • 10
0

You can use form.serialize to send data to your controller.

$(document).ready( function() {
      var form = $('#form2');


        $.ajax( {
          type: "POST",
          url: form.attr( 'action' ),
          data: form.serialize(),
          success: function( response ) {
            console.log( response );
          }
        } );


    } );
Dragon
  • 1,078
  • 2
  • 9
  • 31
  • yes it will send the form elements except input file I want to send form elements and input file to my controller, input file contains the picture – Fahad Meraj Oct 03 '15 at 21:50
  • why are you not adding your data(picture) in your form? – Dragon Oct 03 '15 at 21:55
  • I have tried that but could not succeed – Fahad Meraj Oct 03 '15 at 22:03
  • By your code I guess that you are trying to add a picture with picture data.If this is the case then you should recieve both in a form2 , there might be something missing in your html.plz update that – Dragon Oct 03 '15 at 22:11
  • Also you can add your HttpPostedFileBase myImage in your product viewmodel and you will get everything in Product.Also have you tried passing only picture to controller just for testing – Dragon Oct 03 '15 at 22:13
  • Thank you Sadaquat but now its giving me this error "the input is not a valid Base-64 string as it contains a non-base 64 character" – Fahad Meraj Oct 03 '15 at 22:18