【问题标题】:Mvc doesnt validate input type fileMvc 不验证输入类型文件
【发布时间】:2011-07-13 16:09:03
【问题描述】:

我的视图使用强类型视图模型,验证适用于所有文本字段,但不适用于文件上传,以下是代码:

        <div class="bg-content-inner">
            <% Html.EnableClientValidation(); %>
<% using (Html.BeginForm("Create", "Track", FormMethod.Post, new { enctype = "multipart/form-data" }))
                  { %>
    <%: Html.ValidationSummary("Please Correct the errors and try again")%>
    <table cellpadding="2" cellspacing="2" border="0">

                    <tr>
                        <td style="width:100px;">



        <div class="editor-label">
            <%: Html.LabelFor(model => model.Name) %>
        </div>
        </td>

                        <td colspan="2">
        <div class="editor-field">
            <%: Html.TextBoxFor(model => model.Name, new { style = "width:300px;" })%>
            <%: Html.ValidationMessageFor(model => model.Name,"Circuit Name Required") %>
        </div>

         </td>
                    </tr>

                      <tr>
                        <td>
                           Main Image
                        </td>
                        <td>
                        <div class="editor-field">
                            <input type="file" name="files" id="file1" style="color:White" />
                            <%:Html.ValidationMessageFor(model => model.ImageLarge,"Required") %>
                            </div>
                        </td>
                    </tr>
                    <tr>
                        <td>
                            Detail Image
                        </td>
                        <td>
                        <div class="editor-field">
                            <input type="file" name="files" id="file2" style="color:White" />
                            <%:Html.ValidationMessageFor(model => model.ImageSmall,"Required") %>
                            </div>
                        </td>
                    </tr>

                    <tr></table>

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2 validation


    【解决方案1】:

    如果您使用不显眼的验证,HtmlHelpers 将插入一些 data-XXXX 属性以启用客户端-side 验证...因为 MVC 没有 INPUT[FILE] 的 HtmlHelper,您必须手动 插入 INPUT 元素...您也可以自己添加 data-XXXX 属性...它们和客户端验证将起作用(嗯...在至少在 FF 和 Chrome 中......我没有在其他人身上测试过)......所以......

    替换:

    <input type="file" name="files" id="file2" style="color:White" />
    

    与:

    <input type="file" name="files" id="file2" style="color:White" data-val="true" data-val-required="File is required" />
    

    希望对你有所帮助。

    【讨论】:

    • 这有效,您可以使用此处接受的答案中描述的扩展方法从模型属性中提取消息(最后更新)stackoverflow.com/questions/10970184/…
    • 这太简单了,太强大了!它将验证扩展到不仅仅是模型的输入控件部分。非常感谢分享!
    【解决方案2】:

    我认为验证消息正在寻找 ImageLarge 和 ImageSmall 进行验证。 如果您更改 name 和 id 属性以匹配模型图像名称,它是否有效?例如
    name="ImageLarge" id="ImageLarge"

    【讨论】:

    • 好吧,如果我不能将名称更改为 imagelarge,因为我在创建控制器中反手使用它,例如:foreach(文件中的 HttpPostedFileBase 文件)......
    • 这可能对 jQuery 有帮助,上面的链接中还有一个纯 JavaScript 版本:stackoverflow.com/questions/4222502/…
    【解决方案3】:

    您不能在客户端验证&lt;input type="file" /&gt;;它必须发布到服务器并检查上传,没有办法解决这个问题。

    【讨论】:

    • 你的意思是在 mvc 中我无法验证 html 文件上传,如果有人选择了文件
    • 不,至少没有内置验证。也许可以做一些javascript技巧来手动检查。
    • 你有任何关于如何使用 jquery 实现的教程或示例
    【解决方案4】:

    可能是AjaxSubmit 帮助你。

    【讨论】:

      猜你喜欢
      • 2011-03-10
      • 1970-01-01
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 2023-03-15
      • 2018-12-28
      • 2014-02-12
      • 1970-01-01
      相关资源
      最近更新 更多