【问题标题】:required attribute for image file图像文件的必需属性
【发布时间】:2013-03-15 11:28:20
【问题描述】:

您好,我遇到了 2 个问题:

  1. 我正在使用 requiredif 属性,该属性非常适合其他类型,如文本框、复选框等,但对于这个它不起作用

    [RequiredIf("DoYouAgree", ErrorMessage = "请上传图片徽标。")] [DisplayName("上传徽标")] 字符串 imgfile { 获取;放; }

视图代码:

 @*Browse File*@
     <div class="clear"></div>
    <div class="editor-label grid_2">
     <br /><br />   @Html.LabelFor(model => model.imgfile):
    </div>
    <div style="margin-top:38px;" class="grid_2">
        <input type="file" name="imgfile" id="imgfile" />
    </div>
     <div class="editor-label1 grid_3" style="margin-top:40px;margin-left:80px;" >
        Max file size 299*299 px or 44KB
    </div>
    <br /><br /><br /><br />
    <div class="grid_3 error long" style="margin-left:10px;">
    @Html.ValidationMessageFor(model => model.imgfile)
    </div>
    <br />

为什么它不适合这个。

  1. 我有其他文件只需要必需的属性,但是当我点击我的按钮而不上传图片时它也不起作用

提前致谢!

[Required(ErrorMessage = "Please upload screenshot image.")]
 [DisplayName("Screen Shots")]
 public string ssimgfile { get; set; }

查看:

div style="margin-top:38px; margin-bottom:20px;" class="grid_2">
        <input type="file" name="ssimgfile" id="ssimgfile" />
            <div class="grid_3 error long" style="margin-left:4px;">
             @Html.ValidationMessageFor(model => model.ssimgfile)
            </div>
</div>

【问题讨论】:

  • 有谁知道解决办法???
  • 我知道文件的 js 验证。它不是一个属性,但它会完成这项工作
  • 感谢我用 JS 完成了我的工作

标签: asp.net-mvc razor


【解决方案1】:
I have done my work with javascript:


    $(function () {
        $('form').submit(function () {
            if ($("input[type=radio]:checked").val() == "True") {
                //Logo Image
                var selectedFile = $('#imgfile').val();
                if (selectedFile == "") {
                    alert('please select valid image logo for marketplace');
                    return false;
                }
                else {
                    var fileExtension = ['jpeg', 'jpg', 'png', 'gif', 'bmp'];
                    if ($.inArray($('#imgfile').val().split('.').pop().toLowerCase(), fileExtension) == -1) {
                        alert("You are permited to select only '\n.jpeg','.jpg', '.png', '.gif', '.bmp' formats are allowed.");
                        return false;
                    }
                }

【讨论】:

    猜你喜欢
    • 2015-11-25
    • 2018-05-03
    • 2011-07-13
    • 2011-03-01
    • 2012-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-12
    相关资源
    最近更新 更多