but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable`1[MvcApplication1.Models.FileStore]'
I am following http://www.mikesdotnetting.com/Article/125/ASP.NET-MVC-Uploading-and-Downloading-Files. Using VS2010, ASP.NET 4.0, MVC3 in C# with ADO.NET in SQL Server 2008R2. I am getting the following error message...
The model item passed into the dictionary is of type 'MvcApplication1.Models.FileStore', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable1[MvcApplication1.Models.FileStore]'.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'MvcApplication1.Models.FileStore', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable
1[MvcApplication1.Models.FileStore]'开发者_开发技巧.
Source Error: An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
I am little concern I may have got my Index method wrong. Thanks in advance if anyone can tell me the solution this problem.
Index method in the controller has the following code...
FileStore db = new FileStore();
public ActionResult Index()
{
var db = new FileStore();
return View(db);
}
Create method in the controller has the following method...
public ActionResult Create()
{
foreach (string upload in Request.Files)
{
if (!Request.Files[upload].HasFile()) continue;
string mimeType = Request.Files[upload].ContentType;
Stream fileStream = Request.Files[upload].InputStream;
string fileName = Path.GetFileName(Request.Files[upload].FileName);
int fileLength = Request.Files[upload].ContentLength;
byte[] fileData = new byte[fileLength];
fileStream.Read(fileData, 0, fileLength);
const string connect = @"Server=.\SQLExpress;Database=FileTest;Trusted_Connection=True;";
using (var conn = new SqlConnection(connect))
{
var qry = "INSERT INTO FileStore (FileContent, MimeType, FileName) VALUES (@FileContent, @MimeType, @FileName)";
var cmd = new SqlCommand(qry, conn);
cmd.Parameters.AddWithValue("@FileContent", fileData);
cmd.Parameters.AddWithValue("@MimeType", mimeType);
cmd.Parameters.AddWithValue("@FileName", fileName);
conn.Open();
cmd.ExecuteNonQuery();
}
}
return View();
}
and GetFile method has the following code...
public FileContentResult GetFile(int id)
{
SqlDataReader rdr; byte[] fileContent = null;
string mimeType = ""; string fileName = "";
const string connect = @"Server=.\SQLExpress;Database=FileTest;Trusted_Connection=True;";
using (var conn = new SqlConnection(connect))
{
var qry = "SELECT FileContent, MimeType, FileName FROM FileStore WHERE ID = @ID";
var cmd = new SqlCommand(qry, conn);
cmd.Parameters.AddWithValue("@ID", id);
conn.Open();
rdr = cmd.ExecuteReader();
if (rdr.HasRows)
{
rdr.Read();
fileContent = (byte[])rdr["FileContent"];
mimeType = rdr["MimeType"].ToString();
fileName = rdr["FileName"].ToString();
}
}
return File(fileContent, mimeType, fileName);
}
Index.cshtml like has the following code...
@model IEnumerable<MvcApplication1.Models.FileStore>
Your view is strongly typed to a FileStore collection(a list for example). You should change that if you are sending only one object. Otherwise first create a collection, add the objects to it, then send it to the view.
If you are going with the first method,look up to the first line in your Index View.There should be a statement like this.
Inherits="System.Web.Mvc.ViewPage<IEnumerable<MvcApplication1.Models.FileStore>>
I took it from my view, it may be different in MVC 3. It states view is strongly typed to FileStore collection.Change it
Inherits="System.Web.Mvc.ViewPage<MvcApplication1.Models.FileStore>
Update:
You are using Razor. I am not familiar with that but you should change the model line:
@model MvcApplication1.Models.FileStore
or
@model IEnumerable<MvcApplication1.Models.FileStore>
If you need to send more than one of these objects, create a list in action method, add whatever you want or let the repository get them for you.
List<FileStore> fileStores=new List<FileStore>();
FileStore fileStore=new FileStore();
fileStores.Add(fileStore);
return View(fileStores);
or
var someFileStores=repository.GetSomeStores();
return View(someFileStores);
精彩评论