[英]How to make byte[] field required field properly?
我需要在模型中验证一个byte[]
为Required
但每当我使用Data Annotation
[Required]
时,它都不会做任何事情。 即使我选择了一个文件,它也会输出错误信息。
细节:
模型:
Public class MyClass
{
[Key]
public int ID {get; set;}
[Required]
public string Name {get; set;}
public byte[] Image {get; set;}
[Required]
public byte[] Template {get; set;}
}
视图:
<div class="editor-label">
<%:Html.LabelFor(model => model.Image) %>
</div>
<div class="editor-field">
<input type="file" id="file1" name="files" />
</div>
<div class="editor-label">
<%:Html.Label("Template") %>
</div>
<div class="editor-field">
<input type="file" id="file2" name="files"/>
</div>
<p>
<input type="submit" value="Create" />
</p>
我查看了帖子并注意到人们使用自定义验证,但他们使用HttpPostedFileBase
作为文件类型而不是像我这样的byte[]
,出于某种原因,当我尝试使用相同的错误时,它会丢失ID。即使模型声明了它自己的ID。
编辑:
Context - OnModelCreating
Report
modelBuilder.Entity<Report>().Property(p => p.Image).HasColumnType("image");
modelBuilder.Entity<Report>().Property(p => p.Template).HasColumnType("image");
请注意,我必须将image
作为ColumnType
因为Byte array truncation to a length of 4000.
错误。
控制器:
public ActionResult Create(Report report, IEnumerable<HttpPostedFileBase> files)
{
if (ModelState.IsValid)
{
db.Configuration.ValidateOnSaveEnabled = false;
if (files.ElementAt(0) != null && files.ElementAt(0).ContentLength > 0)
{
using (MemoryStream ms = new MemoryStream())
{
files.ElementAt(0).InputStream.CopyTo(ms);
report.Image = ms.GetBuffer();
}
}
if (files.ElementAt(1) != null && files.ElementAt(1).ContentLength > 0)
{
using (MemoryStream ms1 = new MemoryStream())
{
files.ElementAt(1).InputStream.CopyTo(ms1);
report.Template = ms1.GetBuffer();
}
}
db.Reports.Add(report);
db.SaveChanges();
//Temporary save method
var tempID = 10000000 + report.ReportID;
var fileName = tempID.ToString(); //current by-pass for name
var path = Path.Combine(Server.MapPath("~/Content/Report/"), fileName);
files.ElementAt(1).SaveAs(path);
db.Configuration.ValidateOnSaveEnabled = true;
return RedirectToAction("Index");
}
希望你可能会注意到我所缺少的东西。
RequiredAttribute
检查null和空字符串。
public override bool IsValid(object value)
{
if (value == null)
return false;
string str = value as string;
if (str != null && !this.AllowEmptyStrings)
return str.Trim().Length != 0;
else
return true;
}
如果您的字节数组为空,这可以正常工作,但您可能也想检查一个空数组(没有看到您如何分配您的Template
属性的值,我只能猜测是这种情况)。 您可以定义自己的必需属性来执行此检查。
public class RequiredCollectionAttribute : RequiredAttribute
{
public override bool IsValid(object value)
{
bool isValid = base.IsValid(value);
if(isValid)
{
ICollection collection = value as ICollection;
if(collection != null)
{
isValid = collection.Count != 0;
}
}
return isValid;
}
}
现在,只需更换Required
您的属性Template
与我们的新的属性RequiredCollection
属性。
[RequiredCollection]
public byte[] Template {get; set;}
我查看了帖子并注意到人们使用自定义验证,但他们使用HttpPostedFileBase作为文件类型而不是像我这样的byte []。
是否要将发布的文件绑定到模型中的byterray字段? 如果是,您必须使用自定义模型绑定器。
ASP.NET MVC已经为字节数组内置模型绑定,所以你可以轻松地扩展,作为在此建议后 。
public class CustomFileModelBinder : ByteArrayModelBinder
{
public override object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext)
{
var file = controllerContext.HttpContext.Request.Files[bindingContext.ModelName];
if (file != null)
{
if (file.ContentLength != 0 && !String.IsNullOrEmpty(file.FileName))
{
var fileBytes = new byte[file.ContentLength];
file.InputStream.Read(fileBytes, 0, fileBytes.Length);
return fileBytes;
}
return null;
}
return base.BindModel(controllerContext, bindingContext);
}
}
protected void Application_Start()
{
...
ModelBinders.Binders.Remove(typeof(byte[]));
ModelBinders.Binders.Add(typeof(byte[]), new CustomFileModelBinder());
}
现在,上传的文件将直接作为bytearray存在于属性中。
我改变了我的Create
方法,这就是我想出来的。 似乎工作正常,但......
if (files.ElementAt(1) != null && files.ElementAt(1).ContentLength > 0)
{
using (MemoryStream ms1 = new MemoryStream())
{
files.ElementAt(1).InputStream.CopyTo(ms1);
report.Template = ms1.GetBuffer();
}
}
else // this part of code did the trick, although not sure how good it is in practice
{
return View(report);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.