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.
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With