在 HttpPostedFileBase 中使用正则表达式进行图像验证在 ModelState.IsValid 中始终为 false
我有一个使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我建议在您的控制器方法上放置一个断点并检查您的 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.