ModelState.IsValid=上传图片时为假(bootstrap/Net核心MVC)

ModelState.IsValid= False when upload image(bootstrap/Net core MVC)

我正在尝试上传图片,这是视图:

@model CrearAmigoModelo

@{
    ViewBag.Title = "Nuevo amigo";
}

<form asp-controller="Home" asp-action="Create" method="post">
    <div asp-validation-summary="All" class="text-danger"> </div>
    <div class="form-group row">
        <label asp-for="Nombre" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <input asp-for="Nombre" class="form-control" placeholder="Nombre" />
            <span asp-validation-for="Nombre" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group row">
        <label asp-for="Email" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <input asp-for="Email" class="form-control" placeholder="E-mail" />
            <span asp-validation-for="Email" class="text-danger"></span>
        </div>
    </div>
    <div class="form-group row">
        <label asp-for="Ciudad" class="col-sm-2 col-form-label"></label>
        <div class="col-sm-10">
            <select asp-for="Ciudad" class="custom-select mr-sm-2" asp-items="Html.GetEnumSelectList<Provincia>()">
                :
                <option value="">Seleccione uno</option>
            </select>
            <span asp-validation-for="Ciudad" class="text-danger"></span>
        </div>
    </div>

    <div class="form-group row">
        <label asp-for="Foto" class="col-sm-2 col-form-label">-------------------------</label>
        <div class="col-m-10">
            <div class="custom-file">
                <input type="file" asp-for="Foto" class="form-control custom-file-input"/>
                <label class="custom-file-label"> Selecciona un fichero </label>
            </div>
        </div>
    </div>

    
    <div class="form-group row">
        <div class="col-sm-10">
            <button type="submit" class="btn btn-primary">Nuevo</button>
        </div>
    </div>


    @section Scripts{
        <script>
            $(document).ready(function() {
                $('.custom-file-input').on("change", function (){
                     var fileName=$(this).val().split("\").pop();
                     $(this).next('.custom-file-label').html(fileName);
                });
            });
        </script>
    }
</form>

这是控制器中的方法:


[HttpPost]
        public IActionResult Create(CrearAmigoModelo a)
        {
            if (ModelState.IsValid)
            {
                string guidImagen = null;
                if (a.Foto != null)
                {
                    string ficherosImagenes = Path.Combine(_env.WebRootPath, "images");
                    guidImagen = Guid.NewGuid().ToString() + a.Foto.FileName;
                    string rutaDefinitiva = Path.Combine(ficherosImagenes, guidImagen);
                    a.Foto.CopyTo(new FileStream(rutaDefinitiva, FileMode.Create));
                }
                    
                Amigo nuevoAmigo = new Amigo();
                nuevoAmigo.Nombre = a.Nombre;
                nuevoAmigo.Email = a.Email;
                nuevoAmigo.Ciudad = a.Ciudad;
                nuevoAmigo.rutaFoto = guidImagen;
                //nuevoAmigo.rutaFoto = "++";
                amigoAlmacen.nuevo(nuevoAmigo);
                return RedirectToAction("details", new { id = nuevoAmigo.Id });
            }
            return View();

        }

控制器在“Foto”字段中获取空值,所有其他字段均正确传递。 在我完成所有字段并按下按钮发送表单时的视图中,始终显示消息“需要照片字段”,即使我已经选择了文件。

要包含文件的表单的编码类型需要为 multipart/form-data

<form enctype="multipart/form-data">
</form>

有关详细信息,请参考以下 Microsoft 文档:

Uploading Files in ASP.Net Core