[英]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.