MVC File upload unobtrusive validation

The solution contains MVC server side validators that validates a HttpPostedFileBase minimum file size(MinimumFileSizeValidator), maximum file size(MaximumFileSizeValidator) and the file extension respectively(ValidFileTypeValidator).

C# (8.1 MB)
 
 
 
 
 
5 Star
(2)
845 times
Add to favorites
9/12/2016
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

C#
@model ProfileModel
@{
    ViewBag.Title = "Profile";
}

@section scripts{
    @Scripts.Render("~/bundles/jqueryval")
    @Scripts.Render("~/bundles/custom-validators")
}

<h2>Profile</h2>
@using (Html.BeginForm("Index", "Profile", FormMethod.Post, new { @class = "form-horizontal", enctype = "multipart/form-data" }))
{
    <div class="form-group">
        @Html.LabelFor(m => m.Username, new { @class = "col-sm-2 control-label" })
        <div class="col-sm-10">
            @Html.TextBoxFor(m => m.Username, new { @class = "form-control", placeholder = "Username" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.FirstName, new { @class = "col-sm-2 control-label" })
        <div class="col-sm-10">
            @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control", placeholder = "First Name" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.LastName, new { @class = "col-sm-2 control-label" })
        <div class="col-sm-10">
            @Html.TextBoxFor(m => m.LastName, new { @class = "form-control", placeholder = "Last Name" })
        </div>
    </div>
    <div class="form-group">
        @Html.LabelFor(m => m.Avatar, new { @class = "col-sm-2 control-label" })
        <div class="col-sm-10">
            @Html.TextBoxFor(m => m.Avatar, new { @class = "form-control", type = "file" })
            @Html.ValidationMessageFor(m => m.Avatar)
        </div>
    </div>    
    <div class="form-group">
        <div class="col-sm-offset-2 col-sm-10">
            <button type="submit" class="btn btn-default">Save</button>
        </div>
    </div>
}