首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >IFormFile为空,Razor

IFormFile为空,Razor
EN

Stack Overflow用户
提问于 2019-06-02 14:05:54
回答 1查看 209关注 0票数 0

我试图绑定一个图像上传,但面临的问题,发送这个图像到控制器。

我正在使用Razor和.net核心2.2。我的控制器

代码语言:javascript
复制
[HttpPost(nameof(MealController))]
        [Authorize(Roles = UserRoles.Moderator)]
        public IActionResult Update(MealModel meal, IFormFile pic)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var changedMeal = _mapper.Map<MealModel, MealDTO>(meal);
                    _mealService.Update(changedMeal, pic);
                }
                catch (Exception e)
                {
                    return NotFound();
                }

                return RedirectToAction("Index");
            }

            return View(meal);
        }

我的表格

代码语言:javascript
复制
<form class="form" method="post" asp-controller="Meal" asp-action="Update">
            <div class="modal-content">
                <div class="modal-header">
                    <h5 class="modal-title" id="exampleModalLabel">Продукт</h5>
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                        <span aria-hidden="true">&times;</span>
                    </button>
                </div>
                <div class="modal-body">
                    <div class="product">
                        <div class="product-img">
                            <div class="selector">
                                <img src="@Model.ImagePath" class="img-thumbnail" id="selectedImg" alt="Product Image"/>
                                <div class="form-group">
                                    <input type="file" class="custom-file-input" id="pic-input" name="pic"
                                           onchange="readURL(this, 'selectedImg')">
                                    @* <label class="custom-file-label" for="pic-input"></label> *@
                                </div>
                            </div>
                        </div>
                        <div class="product-info">
                            <div class="form-group name">
                                <label for="name">Назва продукту</label>
                                <input class="form-control" type="text" placeholder="Назва" id="name" name="name" asp-for="Name"
                                       value="@Model.Name">
                            </div>

                            <div class="form-group">
                                <label for="name">Ціна</label>
                                <input class="form-control" placeholder="Price" id="price" type="number" asp-for="Price"
                                       value="@Model.Price">
                            </div>
                            <div class="form-group">
                                <label for="name">Вага</label>
                                <input class="form-control" id="weight" type="number" asp-for="Weight"
                                       value="@Model.Weight">
                            </div>

                            <div class="comments">
                                <textarea class="form-control" rows="3" cols="5" placeholder="Склад" asp-for="Description" value="@Model.Description"></textarea>
                            </div>
                            <input type="hidden" asp-for="MealGroupId" value="@Model.MealGroupId"/>
                            <input type="hidden" asp-for="Id" value="@Model.Id"/>
                            @* <input type="hidden" asp-for="ImagePath" value="@Model.ImagePath"/> *@
                        </div>
                    </div>

                </div>
                <div class="modal-footer">
                    <div class="action-button">
                        <button type="submit"
                                class="btn btn-success ok-button">
                            <span>Додати</span>
                        </button>
                        <button type="reset"
                                class="btn btn-danger remove-button">
                            <span>Відмінити</span>
                        </button>
                    </div>
                </div>
            </div>
        </form>

当我调试时,IFormFile总是null。我试过使用FromForm,但没有用。我搜索了一下,发现我的name在形式和参数名称上是不同的,但是它有效果。你能给我一点提示一下我怎么解决这个问题吗?谢谢你,祝你今天愉快!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-06-02 15:27:40

您必须将<form>enctype属性设置为multipart/form-data才能将文件发送到服务器。

代码语言:javascript
复制
<form class="form" method="post" asp-controller="Meal" asp-action="Update" enctype="multipart/form-data">
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56415769

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档