简体   繁体   English

Ajax 如何将表单数据连同 ID 一起发送到 Controller

[英]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.我在将表单数据和 ID 发送到 controller 时遇到了一些麻烦。 I'm getting the data to do with the file, just having trouble with the ID of event.target.id;.我正在获取与文件有关的数据,只是对 event.target.id 的 ID 有问题;。

Here is what I have below.这是我下面的内容。

Ajax 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 Controller

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

Thank you.谢谢你。

You need to add id as a parameter in the Action method.您需要在 Action 方法中添加 id 作为参数。

I tested it with your code and its working for me我用你的代码测试了它,它对我有用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用Ajax将文件和表单数据一起发布到MVC控制器? - How to Post file along with form data to MVC controller using Ajax? 将数据发送到ajax控制器 - Send data to ajax controller 如何将表单数据发送到控制器以发出Ajax请求以将数据保存在Db中 - How to send form data to controller to make an ajax request for saving data in Db 如何通过 ajax 将包含表单数据但不单击提交按钮的视图 model 发送到 controller - How do I send a view model to the controller via ajax which includes form data but not by clicking the submit button 如何使用Ajax将Model发送到控制器,而不使用表单? - How to send Model to controller by using Ajax,without using form? 如何在没有Ajax的情况下将数据从View发送到Controller? - How to send data from View to Controller without ajax? 如何使用Ajax将模型数据从视图发送到api控制器 - How to send model data from view to api controller using ajax 如何获取数据 ID 发送到另一个控制器 mvc - How to get the data-id Send to another controller mvc 如何将模型类中的ID与一些​​表单数据一起传递给控制器​​? - How to pass the ids in model class in view to the controller along with some form data? 使用 asp.net 内核 6.0 在弹出窗口中提交表单数据不会向 Controller 发送 AJAX 请求 - Submit Form Data in popup using asp.net core 6.0 Doesn't send AJAX Request to Controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM