“HttpPostedFileWrapper”上的 MVC 模型验证

发布于 2024-11-05 19:18:14 字数 1285 浏览 1 评论 0 原文

有人可以帮助我对 HttpPostedFileWrapper 对象进行模型验证吗?

模型:

[Required(AllowEmptyStrings = false)]
public HttpPostedFileWrapper BlahFile { get; set; }

控制器:

   [HttpPost]
    public ActionResult LoadBlahData(BlahModel blahModel) 
    {
        if (!ModelState.IsValid)
            return RedirectToAction("Index");
    }

cshtml:

@using (Html.BeginForm("LoadBlahData", "Admin", FormMethod.Post, new { @class = "blahhForm", enctype = "multipart/form-data", id = "uploadBlah" }))
{
    <fieldset>
        <legend>Upload Blah Information</legend>
        @Html.LabelFor(x=>x.BlahFile, "Upload Blah file:")
        <input size="26" class="uploader" type="file" name="BlahFile" />
        <p><input class="ttButton" type="submit" value="Load Stuff" /></p>
    </fieldset>
}

问题:

  1. 无法看到添加到的“data-val*”属性html。
  2. 不关闭不显眼的验证(输入框上的红色边框)

注释:

  1. 模型中的其他项目在验证方面工作正常,只有 ; 这似乎有问题。
  2. 进入操作方法很好 - (即 - 如果我愿意,我可以访问 InputStream)。
  3. 所有脚本都被正确引用(它适用于普通文本输入)

提前致谢,

Can someone help me with model validation on a HttpPostedFileWrapper object?

Model:

[Required(AllowEmptyStrings = false)]
public HttpPostedFileWrapper BlahFile { get; set; }

Controller:

   [HttpPost]
    public ActionResult LoadBlahData(BlahModel blahModel) 
    {
        if (!ModelState.IsValid)
            return RedirectToAction("Index");
    }

cshtml:

@using (Html.BeginForm("LoadBlahData", "Admin", FormMethod.Post, new { @class = "blahhForm", enctype = "multipart/form-data", id = "uploadBlah" }))
{
    <fieldset>
        <legend>Upload Blah Information</legend>
        @Html.LabelFor(x=>x.BlahFile, "Upload Blah file:")
        <input size="26" class="uploader" type="file" name="BlahFile" />
        <p><input class="ttButton" type="submit" value="Load Stuff" /></p>
    </fieldset>
}

Problem:

  1. Cannot see the "data-val*" attributes being added to the html.
  2. Does not set the unobtrusive validation off (red border on input box)

Notes:

  1. Other items in the Model are working fine with validation, its only the <input type="file"/> that seems to be having problems.
  2. Comes into the action method fine - (i.e - i can access the InputStream if i want).
  3. All scripts are referenced correctly (its working on normal text input's)

Thanks in advance,

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

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

发布评论

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

评论(2

舂唻埖巳落 2024-11-12 19:18:14

对于遇到这个问题的其他人,您也可以这样做 -

<%: Html.TextBoxFor(x => x.BlahFile, new { type = "file" }) %>

Just for anyone else coming across this question you can also do this -

<%: Html.TextBoxFor(x => x.BlahFile, new { type = "file" }) %>
汐鸠 2024-11-12 19:18:14

您尚未向元素添加任何数据属性。添加一下就好了,

<input data-pk="1" size="26" class="uploader" type="file" name="BlahFile" />

不支持的验证

you have not added any data attributes to your element. Add it like,

<input data-pk="1" size="26" class="uploader" type="file" name="BlahFile" />

and there is no support of validation of <input type="file"

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