【问题标题】:Custom HttpAttribute Validation when override IsValid(object value) is always null覆盖 IsValid(对象值)始终为空时的自定义 HttpAttribute 验证
【发布时间】:2013-06-21 01:46:19
【问题描述】:

您好,我有此自定义验证,但要验证的属性类型略有不同,即使我符合业务规则,ModelState 的值也始终为“null”=s ...这里有一些代码

型号

[System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "image")]
        [ValidateFile(Allowed = new string[] { ".png", ".jpeg", ".jpg" },
                      MaxLength = 1024 * 1024 * 3,
                      ErrorMessage = "Please select a PNG , JPEG , JPG image smaller than 3MB")]
public byte[] Photo { get; set; }

============

验证文件属性

public class ValidateFileAttribute : RequiredAttribute
{
    public int MaxLength { get; set; }
    public string[] Allowed { get; set; }

    public override bool IsValid(object **value**)
    {


        var Photo = value as HttpPostedFileBase;
        if (Photo == null)
        {
            return false;
        }

        if (Photo.ContentLength > MaxLength)
        {
            return false;
        }

        if (!Allowed.Contains(Photo.FileName.Substring(Photo.FileName.LastIndexOf('.'))))
        {
            return false;
        }

        return true;

    }

这里的值不应该为空!!

创建.cshtml

@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)


    <fieldset>
        <legend>Restaurant</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Name)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Name)
            @Html.ValidationMessageFor(model => model.Name)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.City)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.City)
            @Html.ValidationMessageFor(model => model.City)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Country)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Country)
            @Html.ValidationMessageFor(model => model.Country)
        </div>

         <div class="editor-label">
            @Html.LabelFor(model=>model.Photo)
        </div>
      <div class="editor-field">
        @Html.EditorFor(model=>model.Photo)
        @Html.ValidationMessageFor(model => model.Photo)
        <input name="ImageFile" type="file" id="ImageFile"  />
      </div>
        <p>
            <input type="submit" value="Create"  />
        </p>

    </fieldset>
}

有什么帮助吗?

【问题讨论】:

    标签: asp.net-mvc validation razor


    【解决方案1】:

    您的财产:

    public byte[] Photo { get; set; }
    

    在属性中:

    var Photo = value as HttpPostedFileBase;
    

    这将始终为空。试试:

    var Photo = value as byte[]
    

    当然,您必须修复验证逻辑的附加部分。

    【讨论】:

    • 感谢您的回答,我确实尝试了您发布的内容并修复了一些逻辑,但即使我将 var Photo = value as byte[] 更改为 null []
    • 那么你没有为 Photo 属性分配任何东西。
    【解决方案2】:

    我会检查您在 byte[] Photo 中放置的内容,因为我注意到当输入的数据无效时,“对象值”为空。

    例如,对于 int 数据类型,当我输入 2147483647 时,“对象值”确实包含 2147483647。

    当我输入 2147483648 时,value 参数包含 0,因为最大的正整数值为 2147483647。

    2013 年 7 月 5 日更新

    您需要将数据类型更改为“HttpPostedFileBase”

    公共 HttpPostedFileBase 照片 { get;放; }

    而不是“字节[]”

    公共字节[]照片{得到;放; }

    应该不需要

    [System.ComponentModel.DataAnnotations.Schema.Column(TypeName = "image")]

    你的

    (对象**值**)

    现在应该包含 System.Web.HttpPostedFileWrapper,其中包含要上传的文件。

    这与我之前写的一致,如果传入的数据类型不是正确的类型,您将得到 null

    【讨论】:

    • 对,那么如何使我尝试上传的图像有效?
    • 我在上面更新了我的答案。如果你让它工作,请告诉我。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    • 1970-01-01
    • 2020-12-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-15
    相关资源
    最近更新 更多