- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我让用户能够在添加/编辑产品模式中为零售产品添加图像。
public class ProductModalViewModel
{
public ProductModalViewModel()
{
Product = new ProductDTO();
Images = new List<ProductImageViewModel>();
}
public ProductDTO Product { get; set; }
public List<ProductImageViewModel> Images { get; set; }
}
每个产品图片都包含在自己的 ViewModel 中,如下所示:
public class ProductImageViewModel
{
public ProductImageViewModel()
{
ProductImage = new ProductImageDTO();
}
public ProductImageDTO ProductImage { get; set; }
[DataType(DataType.Upload)]
public IFormFile ImageFile { get; set; }
}
在提交用于保存产品(和任何添加的图像)的表单后,我的请求被挂起并在 Chrome 开发工具中显示“待定”。我的 Controller 操作从未达到。
只有在我的项目中包含 ProductImage Fields/ViewModel/Logic 时才会发生这种情况。在将该功能添加到模态之前不会发生这种情况,如果我删除所有 ProductImage 字段/ViewModel/Logic 然后再次提交,则工作正常。
将我的 IFormFile 包含在嵌套的 ViewModel 中是否存在固有错误?或者这可能是别的东西。我的其余相关代码如下。
[HttpPost]
public IActionResult SaveProduct([FromForm]ProductModalViewModel model)
{
//save code
}
<form id="formSaveProduct" onsubmit="SaveProduct(event)" enctype="multipart/form-data">
//Other product fields removed for brevity
<div class="row">
<div class="col-md-12">
<ul class="list-group" id="image-list-group">
@for (int i = 0; i < Model.Images.Count(); i++)
{
<partial name="_ImageListItem" for="Images[i]" />
}
</ul>
</div>
</div>
</form>
<li class="list-group-item my-1">
<input type="hidden" asp-for="ProductImage.Id" class="image-id" />
<input type="hidden" asp-for="ProductImage.ProductId" class="image-productid" />
<div class="row">
<div class="col-3">
<input type="text" readonly asp-for="ProductImage.Order" class="image-order" />
</div>
<div class="col-6">
<img src="..\@Model.ProductImage.Path" class="image-display" />
</div>
</div>
</li>
function SaveProduct(e) {
e.preventDefault(); // prevent standard form submission
$.ajax({
url: "@Url.Action("SaveProduct", "ProductManagement", new { Area = "Admin" })",
method: "post",
data: new FormData($('#formSaveProduct')[0]),
contentType: false,
processData: false,
cache: false,
success: function (result) {
//removed for brevity
}
});
}
最佳答案
首先你不需要这个
[DataType(DataType.Upload)]
public IFormFile ImageFile { get; set; }
所以你可以把你的代码改成
public IFormFile ImageFile { get; set; }
并且在你的脚本中你应该添加contentType
function SaveProduct(e) {
e.preventDefault(); // prevent standard form submission
$.ajax({
url: "@Url.Action("SaveProduct", "ProductManagement", new { Area = "Admin" })",
method: "post",
data: new FormData($('#formSaveProduct')[0]),
contentType: false,
processData: false,
cache: false,
contentType: "multipart/form-data", //here
success: function (result) {
if (result.success) {
$("#exampleModal").modal('toggle');
location.reload();
}
else {
$(".modal-body").html(result);
}
}
});
}
关于javascript - .NET Core ViewModel 中的 IFormFile 属性导致 AJAX 请求停滞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56334056/
我有一个物理文件,我想将它传递给需要 IFormFile 类型(用于单元测试)的 Controller 方法。 我找不到任何实例化 IFormFile 接口(interface)的类,这意味着我无法创
我正在开发 ASP.NET Core 2 MVC 应用程序,尝试设置服务层。我有一个非常基本的应用程序,我想在其中: 在姓名旁边上传一张图片 向数据库添加一条记录 将文件存储在磁盘上的 wwwroot
我正在尝试通过 IFormFile 读取一个 zip 文件,我正在浏览里面的文件以解压缩选定的扩展名并解压缩它,但我无法使用IFormFile 来读取它。它说无法将 IFormFile 转换为字符串。
我上传了一张图片并想将它发送到第三方服务(Cloudinary)而不将文件保存在我的服务器中。 public async Task> GetImagesUrlsByImage(IFormFile im
[Route("api/file")] [Produces("application/json")] [Consumes("application/json", "application/json-p
我正在使用 C# 和 Net Core 开发一些 REST API 我的存储库中有一个函数接受类型为 IFormFile 的参数. public async Task UploadFile(IForm
[Route("api/file")] [Produces("application/json")] [Consumes("application/json", "application/json-p
是否可以将 IFormFile 文件添加到 .net core 中的电子邮件附件中?我正在使用 formdata 从 Angular 获取文件。 for (let file of this.file
我有以下代码(我仍在开发它)成功地将文件上传到所需的路径。 private async Task AddImage(IFormFile image, string filePath) {
我正在尝试使用 this piece of code 将文件保存在磁盘上. IHostingEnvironment _hostingEnvironment; public ProfileControl
我正在尝试阅读 IFormFile从这样的 HTTP POST 请求中收到: public async Task UploadDocument([FromForm]DataWrapper data)
我正在尝试使用 IFormFile 作为嵌套 ViewModel 中的属性。我在尝试将 ViewModel 绑定(bind)到运行时的 Controller 操作时遇到问题。 AJAX 请求停止并且永
我有一个 .NET Core API 项目,它有一个简单的端点来上传文件: [Route("api/[controller]")] [ApiController] public class Files
我有一个 .NET Core API 项目,它有一个简单的端点来上传文件: [Route("api/[controller]")] [ApiController] public class Files
我的 WebAPI 方法已准备好接受文件作为参数,如下所示:(例如 URI“https://localhost:44397/api/uploadfile” [Route("api/[controll
我用这段代码改变了图像的大小。但此方法返回 FileStreamResult。我想将 FileStreamResult 转换为 IFromFile。我该怎么做? 注意:我正在使用 CoreCompat
在 ASP.Net Core Web API 中为我的一个服务类编写单元测试时,我需要模拟一个 IFormFile。因此我决定像这样模拟它(使用最小起订量): fileMock.Setup(x =>
我有一个 .NET Core 应用程序,它有一个名为 Documents 的 Controller ,带有一个 POST 签名,如下所示: [HttpPost] public async Task P
我正在制作一个可以上传到数据库和从数据库检索图像的 API。第一部分已完成 - 图像存储在 IFormFile 变量中,并作为 bytes[] 传递给数据库。 图片模型: public class I
我有后端端点 Task Post(IFormFile csvFile)我需要从 HttpClient 调用这个端点。目前我得到 Unsupported media type error .这是我的代码
我是一名优秀的程序员,十分优秀!