在 HttpPostedFileBase 中使用正则表达式进行图像验证在 ModelState.IsValid 中始终为 false

发布于 2025-01-12 06:17:22 字数 1839 浏览 0 评论 0原文

我有一个使用 htmlhelper 上传图像字段的表单,但在控制器中它总是使用 ModelState.IsValid 返回 false,我不知道我的代码有什么问题。你能帮助我吗?这是我的代码。

Form.cshtml

@using (Html.BeginForm("AddonSaveLayout", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(x => x.AddonLayoutForm.AddonSetup.Id)
    <div class="box box-primary">
        <div class="box-body">
            <div class="form-group">
                <label>Upload Image </label>
                @Html.TextBoxFor(x => x.AddonLayoutForm.AddonSetup.ImagePath, new { type = "file", @accept = "image/jpg, image/jpeg, image/png" })
                @Html.ValidationMessageFor(model => model.AddonLayoutForm.AddonSetup.ImagePath, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="box-footer">
            @Html.ActionLink("Back", "AddonSetup", null, new { @class = "btn btn-warning" })
            <button type="submit" class="btn btn-primary" id="submitAddCatBtn">Save</button>
        </div>
    </div>
}

Model.cs

 [Required(ErrorMessage = "Please select file.")]
 [RegularExpression(@"([a-zA-Z0-9\s_\\.\-:])+(.png|.jpg|.jpeg)$", ErrorMessage = "Only Image files allowed and should be 5MB or lower.")]
 public HttpPostedFileBase ImagePath { get; set; }

Controller.cs

if (!ModelState.IsValid)
{
    string messages = string.Join("; ", ModelState.Values
                                    .SelectMany(x => x.Errors)
                                    .Select(x => x.ErrorMessage));

     var TicketDesc = db.TicketDescriptions.ToList();
     model.AddonLayoutForm.TicketDescriptions = TicketDesc;
     return View("AddonForm", model);
}

I have a form with the upload image field using htmlhelper but in controller it always return false using ModelState.IsValid and I don't know what's wrong with my code. Can you help me? Here's my code.

Form.cshtml

@using (Html.BeginForm("AddonSaveLayout", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.AntiForgeryToken()
    @Html.HiddenFor(x => x.AddonLayoutForm.AddonSetup.Id)
    <div class="box box-primary">
        <div class="box-body">
            <div class="form-group">
                <label>Upload Image </label>
                @Html.TextBoxFor(x => x.AddonLayoutForm.AddonSetup.ImagePath, new { type = "file", @accept = "image/jpg, image/jpeg, image/png" })
                @Html.ValidationMessageFor(model => model.AddonLayoutForm.AddonSetup.ImagePath, "", new { @class = "text-danger" })
            </div>
        </div>
        <div class="box-footer">
            @Html.ActionLink("Back", "AddonSetup", null, new { @class = "btn btn-warning" })
            <button type="submit" class="btn btn-primary" id="submitAddCatBtn">Save</button>
        </div>
    </div>
}

Model.cs

 [Required(ErrorMessage = "Please select file.")]
 [RegularExpression(@"([a-zA-Z0-9\s_\\.\-:])+(.png|.jpg|.jpeg)
quot;, ErrorMessage = "Only Image files allowed and should be 5MB or lower.")]
 public HttpPostedFileBase ImagePath { get; set; }

Controller.cs

if (!ModelState.IsValid)
{
    string messages = string.Join("; ", ModelState.Values
                                    .SelectMany(x => x.Errors)
                                    .Select(x => x.ErrorMessage));

     var TicketDesc = db.TicketDescriptions.ToList();
     model.AddonLayoutForm.TicketDescriptions = TicketDesc;
     return View("AddonForm", model);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-残月青衣踏尘吟 2025-01-19 06:17:22

我建议在您的控制器方法上放置一个断点并检查您的 ModelState 以查找导致其导致无效 ModelState 的违规属性。

I'd recommend placing a breakpoint on your controller method and examine your ModelState to find the offending property causing it to result in an Invalid ModelState.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文