【问题标题】:Ajax How to send Form Data along with ID to ControllerAjax 如何将表单数据与 ID 一起发送到控制器
【发布时间】:2021-01-14 11:44:38
【问题描述】:

我在将表单数据和 ID 一起发送到控制器时遇到了一些麻烦。我正在获取与文件有关的数据,只是在 event.target.id 的 ID 上有问题;。

这是我下面的内容。

阿贾克斯

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) {


                                }
                            });

控制器

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

谢谢。

【问题讨论】:

    标签: c# ajax model-view-controller


    【解决方案1】:

    需要在 Action 方法中添加 id 作为参数。

    我用你的代码测试了它并为我工作

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

    【讨论】:

      猜你喜欢
      • 2018-06-01
      • 2019-06-21
      • 1970-01-01
      • 2019-07-02
      • 2019-08-28
      • 2017-04-28
      • 2017-02-01
      • 2017-03-11
      • 2016-03-21
      相关资源
      最近更新 更多