3

I hope someone can help me with this.

I am using VS 2012 and MVC4.

I am testing a project using Strongly Typed Model using HttpPostedFileBase. When I try to Scaffold the Views it fails with:

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

Parameter name: key
---------------------------
OK   
---------------------------

I have tried to Un-Install and then Re-Install MVC as was suggested in a few posts on the net but this has not helped. This is my Model: (Yes I have tried [Key] on the Id but makes no difference)

using System;
using System.Web;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace ImageTest.Models
{
public class ImageHandler
{
public int Id { get; set; }
public string ImageName { get; set; }
public HttpPostedFileBase File { get; set; }
}
}

I thought it may be a Context issue but it does not matter if I create a custom Context or use a predefined one I get the same error. This is the pre-defined Context:

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

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

public DbSet<ImageHandler> ImageHandler { get; set; }
}

As a Test, if I comment out:

// public HttpPostedFileBase File { get; set; }

I can scaffold the View with no problem. Is this a bug? I can not see in the documentation anywhere that Scaffolding HttpPostedFileBase is not supported. See: HttpPostedFileBase

Thanks in advance.

Rusty Nail
  • 2,692
  • 3
  • 34
  • 55

2 Answers2

2

Stan is on the right track.

Model-View-Controller or MVC uses the Entity Framework to Scaffold Views.

Entity Data Model: Primitive Data Types

Primitive Data Types are currently supported in .NET 4.5 unless a Complex Data Type is defined. The following are the supported Primitive Data Types:

Binary
Boolean
Byte
DateTime
DateTimeOffset
Decimal
Double
Float
Guid
Int16
Int32
Int64
SByte
String
Time

See: Complex Type for more Information on extending this functionality.

Thanks Stan Thumbs up from me.

EDIT: One needs to Scaffold the View with Primitive Data Types first and then add HttpPostedFileBase to the Model later to use the File Upload Capabilities. As an example see: Upload Image in form and show it on MVC 4

Also you will need to use (NotMapped) in your Model:

[NotMapped]
public HttpPostedFileBase File { get; set; }

Now in the Scaffolded Create ActionResult Method, your View's Form Return Valus contains a System.Web.HttpPostedFileWrapper that you can use.

So Short Answer:

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.

That should be enough to get you working...

Community
  • 1
  • 1
Rusty Nail
  • 2,692
  • 3
  • 34
  • 55
1

I don't think you will be able to HttpPostedFileBase as a property of your model, well at least not have it mapped via EntityFramework and automatically scaffolded. If you think about it - what database fields do you think this property type would map to?

If you want to actually store the binary data in your database, use this

public byte[] File { get; set; }

as your property.

StanK
  • 4,750
  • 2
  • 22
  • 46
  • Thanks Stan, I think you're on the right track. See: [Link](http://msdn.microsoft.com/en-us/library/ff649643.aspx) MVC Model-View-Controller Must have HttpPostedFileBase in the Model to work for File Uploads but it seems the Entity Framework does not recognise its use in the context I have it. It seems I need to add it later after the Scaffolding of the Views and wire it up separately. – Rusty Nail Jun 18 '13 at 04:35
  • Yes - often you will find that the Model in your MVC project and your Entity Model in your Entity Framework model have differing requirements and therefore must be different objects. – StanK Jun 18 '13 at 04:55