Asp.net MVC-输入type =“ file”验证错误

时间:2019-06-05 13:38:38

标签: asp.net asp.net-mvc forms validation razor

我创建了一个包含2个输入文本和1个输入文件的表单,当我选择图像并单击Submit按钮时,输入文件发生错误

我对输入文件没有任何验证!

validation error

这是我的查看代码:

@model Test.Models.Domain.tblCategory
@{
                Layout = null;
}
@using (Html.BeginForm("AddOrEditeCat", "Admin", FormMethod.Post, new {enctype="multipart/form-data",onsubmit="return SubmitCatForm(this)" }))
{
    @Html.AntiForgeryToken()

    @Html.HiddenFor(model => model.id)
    @Html.HiddenFor(model => model.Pic)

    <div class="form-group">
        @Html.Label("Title", new { @class = "control-label" })
        @Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control" } })
        @Html.ValidationMessageFor(model => model.Title)
    </div>

    <div class="form-group">
        @Html.Label("Text", new { @class = "control-label" })
        @Html.EditorFor(model => model.Text, new { htmlAttributes = new { @class = "form-control" } })
    </div>

    <div class="form-group">
    @Html.LabelFor(model=>model.Pic, new { @class = "control-label" })
        <img src="@Url.Content(Model.Pic)" style="margin:10px" height="150" width="150" id="imagePreview" />
        <input type="file" id="ImageUpload" name="ImageUpload" accept="image/jpeg,image/png" onchange="ShowImagePreview(this,document.getElementById('imagePreview'))" />
    </div>

    <div class="form-group">
        <input type="submit" value="Submit" class="btn btn-primary" />
        <input type="reset" value="Reset" class="btn btn-primary btn-danger" />
    </div>

}

这是我的模型:

public partial class tblCategory
{
    public int id { get; set; }
    [Required(ErrorMessage = "*")]
    public string Title { get; set; }
    public string Text { get; set; }
    [DisplayName("Image")]
    public string Pic { get; set; }

    public HttpPostedFileBase ImageUpload { get; set; }

    public tblCategory()
    {
        Pic = "~/Cntent/upload/img/cat/defalt.png";
    }
}

0 个答案:

没有答案