问题在于您的 HttpPostedFile,如果您使用的是 EditorFor(x => x.myfile),它的作用是检查表达式中指定的属性的类型并尝试为该类型找到匹配的编辑器模板。这个模板告诉 Razor 它应该为模板实现的类型的属性生成什么 HTML。 Razor 有几个可以覆盖的模板,它们在项目中不可见,但 HttpPostedFile 不是其中之一,这就是为什么您可能将 FileName、ContentType 和 ContentLength 视为为该 HttpPostedFile 呈现的接口。
下面是那个类 HttpPostedFile 的实现
public abstract class HttpPostedFileBase {
public virtual int ContentLength {
get {
throw new NotImplementedException();
}
}
public virtual string ContentType {
get {
throw new NotImplementedException();
}
}
public virtual string FileName {
get {
throw new NotImplementedException();
}
}
public virtual Stream InputStream {
get {
throw new NotImplementedException();
}
}
[SuppressMessage("Microsoft.Naming", "CA1702:CompoundWordsShouldBeCasedCorrectly", MessageId = "filename",
Justification = "Matches HttpPostedFile class")]
public virtual void SaveAs(string filename) {
throw new NotImplementedException();
}
}
因为其中一个模板不是默认模板,所以 Razor 会探索该类并尝试为该类的每个公共属性生成编辑器。
如果您在属性上指定 DataType
[DataType(DataType.SELECTYOURTYPEHERE)]
然后你可以覆盖上的模板
Views/Shared/EditorTemplates,例如你可以这样做
[DataType(DataType.Upload)]
public HttpPostedFile myfile { get; set; }
然后在 Views/Shared/EditorTemplates/Upload.cshtml 上创建并 Upload.cshtml
使用您要为上传显示的 html。
希望对你有帮助