Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用HttpPostedFileBase的强类型模型无法构建视图_C#_Asp.net Mvc 4 - Fatal编程技术网

C# 使用HttpPostedFileBase的强类型模型无法构建视图

C# 使用HttpPostedFileBase的强类型模型无法构建视图,c#,asp.net-mvc-4,C#,Asp.net Mvc 4,我希望有人能帮我 我正在使用VS 2012和MVC4 我正在使用HttpPostedFileBase使用强类型模型测试一个项目。当我尝试构建视图时,它失败了: --------------------------- Microsoft Visual Studio --------------------------- Unable to retrieve metadata for 'ImageTest.Models.ImageHandler'. Value cannot be null. Pa

我希望有人能帮我

我正在使用VS 2012和MVC4

我正在使用HttpPostedFileBase使用强类型模型测试一个项目。当我尝试构建视图时,它失败了:

---------------------------
Microsoft Visual Studio
---------------------------
Unable to retrieve metadata for 'ImageTest.Models.ImageHandler'. Value cannot be null.

Parameter name: key
---------------------------
OK   
---------------------------
我曾尝试按照网上几篇文章的建议,先卸载,然后再重新安装MVC,但这没有帮助。这是我的模型:(是的,我在Id上尝试了[Key],但没有区别)

我认为这可能是一个上下文问题,但无论是创建自定义上下文还是使用预定义上下文,我都会遇到相同的错误。这是预定义的上下文:

using ImageTest.Models;
using System.Data.Entity;

public class ImageHandlerContext : DbContext
{
public ImageHandlerContext() : base("DefaultConnection")
{
}

public DbSet<ImageHandler> ImageHandler { get; set; }
}
我可以毫无问题地把这一观点概括起来。这是虫子吗?我在文档中看不到任何地方不支持搭建HttpPostedFileBase。见:


提前感谢。

我认为您无法将HttpPostedFileBase作为模型的属性,至少不需要通过EntityFramework将其映射并自动搭建。仔细想想,您认为该属性类型将映射到哪些数据库字段

如果您想在数据库中实际存储二进制数据,请使用

public byte[] File { get; set; }

作为您的财产。

斯坦走上了正确的道路

模型视图控制器或MVC使用实体框架来构建视图

除非定义了复杂的数据类型,否则.NET 4.5当前支持基本数据类型。以下是受支持的基本数据类型:

Binary
Boolean
Byte
DateTime
DateTimeOffset
Decimal
Double
Float
Guid
Int16
Int32
Int64
SByte
String
Time
有关扩展此功能的更多信息,请参阅

谢谢斯坦,我竖起大拇指

编辑:首先需要使用基本数据类型构建视图,然后将HttpPostedFileBase添加到模型中,以使用文件上载功能。例如,请参见:

此外,您还需要在模型中使用(未映射):

[NotMapped]
public HttpPostedFileBase File { get; set; }
现在,在搭建的createactionresult方法中,视图的表单returnvalus包含一个System.Web.HttpPostedFileWrapper,您可以使用它

如此简短的回答:

1: Create your Code First Model with Primitive Data Types only! Unless you use the [NotMapped] Attribute.
2: Scaffold your View's.
3: If not done so in step 1, Add to your Model the Methods needed.  E.G: public HttpPostedFileBase File { get; set; } using the [NotMapped] Attribute
4: Add to your Database the necessary Table either manually or from the Console.
5: Add the necessary code to your View's and Controller.

这应该足够让你工作了……

谢谢斯坦,我认为你走对了方向。请参阅:MVC模型视图控制器必须在模型中具有HttpPostedFileBase才能用于文件上载,但实体框架似乎无法识别其在我拥有的上下文中的用途。看起来我需要在构建视图之后再添加它,并将其单独连接起来。是的-通常您会发现MVC项目中的模型和实体框架模型中的实体模型有不同的要求,因此必须是不同的对象。
[NotMapped]
public HttpPostedFileBase File { get; set; }
1: Create your Code First Model with Primitive Data Types only! Unless you use the [NotMapped] Attribute.
2: Scaffold your View's.
3: If not done so in step 1, Add to your Model the Methods needed.  E.G: public HttpPostedFileBase File { get; set; } using the [NotMapped] Attribute
4: Add to your Database the necessary Table either manually or from the Console.
5: Add the necessary code to your View's and Controller.