无法使用SetModelValue修改ModelState

FLI*_*KER 6 c# model-view-controller asp.net-mvc entity-framework

我正在开发MVC / EF Web应用程序。在其中一种形式中,我编辑一个模型。模型有一个像场(public byte[] BioPhoto)

当我将图像上传到该字段并保存数据时,ModelState.IsValidfalse,因为BioPhoto属性null位于ModelState中。请注意,模型中的BioPhoto已加载图像数据。

我试图使用下面的代码将图片注入ModelState,但仍然ModelState.IsValidfalse

public ActionResult Edit([Bind(Include = "BusinessId,Name,About,Phone,TollFree,FAX,Email,Bio,BioPhoto")] Business business)
{
    if (System.IO.File.Exists("image.jpg"))
    {
        business.BioPhoto = System.IO.File.ReadAllBytes("image.jpg");
        ModelState.SetModelValue("BioPhoto", 
                   new ValueProviderResult(business.BioPhoto, "", System.Globalization.CultureInfo.InvariantCulture));
    }

    if (ModelState.IsValid)
    {
        db.Entry(business).State = EntityState.Modified;
        db.SaveChanges();
        return RedirectToAction("Index");
    }
    return View(business);
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么。有没有更好的解决方案来解决这个问题?

我在SO中读了几个答案,但不知道如何解决我的问题。

这是我的模特

public class Business
{
    public int BusinessId { get; set; }

    [Required]
    [StringLength(100)]
    public string Name { get; set; }

    [Required]
    public Address address { get; set; }

    [Required]
    [StringLength(20)]
    public string Phone { get; set; }

    [StringLength(20)]
    public string TollFree { get; set; }

    [StringLength(20)]
    public string FAX { get; set; }

    [Required]
    [StringLength(50)]
    public string Email { get; set; }

    [Required]
    [StringLength(100)]
    public string WebSite { get; set; }

    [Required]
    public string About { get; set; }

    [Required]
    public string Bio { get; set; }

    [Required]
    public byte[] BioPhoto { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我的看法

<div class="form-group">
    @Html.LabelFor(model => model.BioPhoto, "BIO Photo (Best Size: 350 x 450)", htmlAttributes: new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <form enctype="multipart/form-data">
            <div class="form-group" style="width:400px">
                <input id="BioPhoto" type="file" multiple class="file" data-overwrite-initial="false" />
            </div>
        </form>
        @Html.ValidationMessageFor(model => model.BioPhoto, "", new { @class = "text-danger" })
    </div>
</div>
Run Code Online (Sandbox Code Playgroud)

Cod*_*shi 6

就像我说的,问题是表单没有将其发布BioPhoto到控制器 - 最有可能。您可以使用 Chrome 查看 http 请求正文中的内容。

如果您想添加图像,即使控制器没有接收到图像,请尝试此操作。

尝试仅删除与图像属性相关的错误:

ModelState["BioPhoto"].Errors.Clear();
Run Code Online (Sandbox Code Playgroud)

然后添加图像:

business.BioPhoto = System.IO.File.ReadAllBytes("image.jpg");
Run Code Online (Sandbox Code Playgroud)

然后更新模型:

UpdateModel(business);
Run Code Online (Sandbox Code Playgroud)

现在,如果您调用ModelState.IsValid,它将考虑 的设置BioPhoto并重新评估IsValid

编辑:

我建议打电话

ModelState.SetModelValue("BioPhoto", new ValueProviderResult(business.BioPhoto, "", System.Globalization.CultureInfo.InvariantCulture));
Run Code Online (Sandbox Code Playgroud)

将值实际推送到 ModelState.Values。但这不是必需的。