简体   繁体   中英

Ajax How to send Form Data along with ID to Controller

I'm having a bit of trouble Sending the Form Data along with an ID to the controller. I'm getting the data to do with the file, just having trouble with the ID of event.target.id;.

Here is what I have below.

Ajax

var id = event.target.id;
                
var file = $("#" + event.target.id).prop("files")[0];
var formData = new FormData;
formData.append("file", file);formData.append("id",event.target.id);



                            $.ajax({
                                url: "@Url.Action("SaveImage")",
                                method: "post",
                                contentType: false,
                                processData: false,
                                data: formData,
                                success: function (booSession) {


                                }
                            });

Controller

[HttpPost]
    public ActionResult SaveImage()
    {var file = Request.Files[0];}

Thank you.

You need to add id as a parameter in the Action method.

I tested it with your code and its working for me

        [HttpPost]
        public ActionResult SaveImage(int id)
        { 
            var file = Request.Files[0];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM