如果您的视图基于模型并且您已在其中生成了控件<form>
标签,然后您可以将模型序列化为FormData
using
var formdata = new FormData($('form').get(0));
这也将包括使用生成的任何文件<input type="file" name="myImage" .../>
并使用将其发回
$.ajax({
url: '@Url.Action("YourActionName", "YourControllerName")',
type: 'POST',
data: formdata,
processData: false,
contentType: false,
});
并在你的控制器中
[HttpPost]
public ActionResult YourActionName(YourModelType model)
{
}
或者(如果您的模型不包含以下属性)HttpPostedFileBase
)
[HttpPost]
public ActionResult YourActionName(YourModelType model, HttpPostedFileBase myImage)
{
}
如果您想添加表单中未包含的其他信息,则可以使用
formdata.append('someProperty', 'SomeValue');