我使用的是 .net 4.0,所以不能使用内置的 FileExtensions 属性。
我正在尝试进行自己的验证,但遇到了绊脚石。我发现这篇文章看起来是一个很好的资源:http ://blog.tomasjansson.com/creating-custom-unobtrusive-file-extension-validation-in-asp-net-mvc-3-and-jquery
但唉,我的对象(值)总是以空值出现。我唯一不同的是我没有使用 HttpPostedFileBase 作为我的模型,我使用的是具有其他一些属性的视图模型。
任何想法如何在 IsValid 重载中填充我的对象以便我可以检查它?
这是我的代码,它或多或少是从那篇文章中复制和粘贴的,但我的视图模型中有更多内容:
视图模型:
public class Mp3ViewModel
{
public string FileName { get; set; }
public string FilePath { get; set; }
[Required(ErrorMessage="You must enter a description of the MP3")]
[Display(Name = "Description of MP3:")]
public string Description { get; set; }
[Required(ErrorMessage = "You must enter a job role")]
[Display(Name = "Job Role:")]
public string CallJobRole { get; set; }
[Required(ErrorMessage = "You must enter a call outcome")]
[Display(Name = "Call Outcome:")]
public string CallOutcome { get; set; }
[Required(ErrorMessage = "You must enter a call type")]
[Display(Name = "Call Type:")]
public string CallType { get; set; }
[Required(ErrorMessage = "You must enter a call section")]
[Display(Name = "Call Section:")]
public string CallSection { get; set; }
[Required(ErrorMessage = "You must enter call comments")]
[Display(Name = "Call Comments:")]
public string CallComments { get; set; }
[Required(ErrorMessage = "You must enter call keywords")]
[Display(Name = "Call Keywords (separate by comma):")]
public string CallKeywords { get; set; }
[Required(ErrorMessage = "You must select a file")]
[Display(Name = "Select an MP3 to upload:")]
[FileExtensions("txt|doc")]
public HttpPostedFileBase Mp3 { get; set; }
}
自定义属性验证:
[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property, AllowMultiple = false, Inherited = true)]
public class FileExtensionsAttribute : ValidationAttribute
{
private List<string> ValidExtensions { get; set; }
public FileExtensionsAttribute(string fileExtensions)
{
ValidExtensions = fileExtensions.Split('|').ToList();
}
public override bool IsValid(object value)
{
HttpPostedFileBase file = value as HttpPostedFileBase;
if (file != null)
{
var fileName = file.FileName;
var isValidExtension = ValidExtensions.Any(y => fileName.EndsWith(y));
return isValidExtension;
}
return true;
}
}
看法:
@model CallLibrary.BO.ViewModels.Mp3ViewModel
@{
ViewBag.Title = "Call Library Administration";
Layout = "~/Views/Shared/_Layout.cshtml";
}
@section scripts
{
<script src="~/Scripts/jquery.validate.min.js" type="text/javascript"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.min.js" type="text/javascript"></script>
}
<h2>Call Library Administration</h2>
@ViewBag.test
@using (Html.BeginForm())
{
<div>
@Html.ValidationMessageFor(x => x.Description)<br />
@Html.LabelFor(x => x.Description)
@Html.TextAreaFor(x => x.Description)
</div>
<div>
@Html.ValidationMessageFor(x => x.CallJobRole)<br />
@Html.LabelFor(x => x.CallJobRole)
@Html.TextBoxFor(x => x.CallJobRole)
</div>
<div>
@Html.ValidationMessageFor(x => x.CallOutcome)<br />
@Html.LabelFor(x => x.CallOutcome)
@Html.TextBoxFor(x => x.CallOutcome)
</div>
<div>
@Html.ValidationMessageFor(x => x.CallType)<br />
@Html.LabelFor(x => x.CallType)
@Html.TextBoxFor(x => x.CallType)
</div>
<div>
@Html.ValidationMessageFor(x => x.CallSection)<br />
@Html.LabelFor(x => x.CallSection)
@Html.TextBoxFor(x => x.CallSection)
</div>
<div>
@Html.ValidationMessageFor(x => x.CallComments)<br />
@Html.LabelFor(x => x.CallComments)
@Html.TextAreaFor(x => x.CallComments)
</div>
<div>
@Html.ValidationMessageFor(x => x.CallKeywords)<br />
@Html.LabelFor(x => x.CallKeywords)
@Html.TextAreaFor(x => x.CallKeywords)
</div>
<div>
@Html.ValidationMessageFor(x=>x.Mp3)<br />
@Html.LabelFor(x=>x.Mp3)
@Html.TextBoxFor(x=>x.Mp3, new {type= "file"})
</div>
<div>
<input type="submit" value="Add MP3" />
</div>
}
任何建议将不胜感激。TIA