0

I am developing MVC 5 App. I have a Parent View that call a Partial View, where user can Load a Image.

On Submit a call a .Ajax defined in Parent view that call Method/Controller.

What I need is to send to the controller data I have in Parent View. Is that Posible?

Here is my code.

Parent View

enter image description here

Partial View

enter image description here

.Ajax Method

          $('#formPhoto').submit(function (event) {
                event.preventDefault();
                if ($(this).valid()) {
                    var id="aaa";
                    var formdata = new FormData($(this).get(0));
                    $.ajax({
                        url: this.action,
                        type: this.method,
                        data:formdata,
                        processData: false,
                        contentType: false,
                        beforeSend: function () {
                            return true;
                        },
                        success: function (result) {
                            successPhoto();
                        },
                        complete: function () {
                            // alert(3);
                            // And so on.
                        }
                    });
                }
                return false;
            });
            

I need to send var aa='aaa' in data:

Diego
  • 2,238
  • 4
  • 31
  • 68

1 Answers1

1

Yes it's possible. So basically I can suggest for you two ways to do what you need:

1st: You could consider putting the form element in the ParentView and change your code a little bit... 2nd: You could recover the data from the parent view and serialize it together to send to your action.

So, from the second option it would be something like:

var parentInformation = 'aaa';
var formdata = new FormData($(this).get(0));
formdata.ExpectedParentOnPropertySide = parentInformation;

Please, I hope this solve your problem

mfvjunior
  • 468
  • 3
  • 15
  • Thanks.... How do I get the data in the Controller? I get the Model from Parent View and Image from Partial View like this ChangePhoto(ModelParentView model, System.Web.HttpPostedFileBase Image = null)... Surely I would change my model as you say in 1st... – Diego Feb 25 '17 at 15:24
  • You could create a class to receive both information in a single class. Inside ModelParentView you can create a property that will receive the image.For a better comprehension from what is happening, check your post request to server, and identify each property beeing posted to your server and how it will be binded to your server action parameter. – mfvjunior Feb 25 '17 at 15:45