2017-05-05 70 views
2
using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.ComponentModel.DataAnnotations.Schema; 
using System.Linq; 
using System.Web; 

    namespace Church_On_The_Rock.Models 
{ 
    public class News 
{ 
    public int NewsId { get; set; } 
    public string Title { get; set; } 
    public string Writer { get; set; } 
    [DisplayFormat(DataFormatString = "{0:MMMM dd, yyyy}", 
    ApplyFormatInEditMode = true)] 
    public DateTime Date { get; set; } 
    public byte[] Picture { get; set; } 

    public string Blog { get; set; } 

    } 
    //I know the problem is here but don't know what to do 
    public class ExtendedNewsModel: News 
    { 
    public HttpPostedFileBase postedPicture { get; set; } 
    } 

} 

這裏是我的數據訪問代碼:值不能爲空。參數名:EntitySet的

public ActionResult Create(ExtendedNewsModel news) 
    { 
     if (ModelState.IsValid) 
     { 
      if (news.postedPicture != null) 
      { 

       if (news.postedPicture.ContentLength > (8 * 1024 * 1024)) 
       { 
        ModelState.AddModelError("CustomError", "Image can not be larger than 8MB."); 
        return View(); 
       } 
       if (!(news.postedPicture.ContentType == "image/jpeg" || news.postedPicture.ContentType == "image/png" || news.postedPicture.ContentType == "image/gif")) 
       { 
        ModelState.AddModelError("CustomError", "Image must be in jpeg,png or gif format"); 
       } 

      } 
      byte[] data = new byte[news.postedPicture.ContentLength]; 
      news.postedPicture.InputStream.Read(data, 0, news.postedPicture.ContentLength); 
      news.Picture = data; 
      db.New.Add(news); 
      db.SaveChanges(); 
      return RedirectToAction("Index"); 
     } 

     return View(news); 
    } 

這是我的觀點:

@model Church_On_The_Rock.Models.ExtendedNewsModel 

    @{ 
    ViewBag.Title = "Create"; 
    } 
    @using (Html.BeginForm("Create","Home", FormMethod.Post, new {role = "form", enctype = "multipart/form-data"})) 
    { 

    @Html.AntiForgeryToken() 
    <div class="form-group createforms"> 
     <span>@Html.LabelFor(model => model.Picture, htmlAttributes: new { 
     @class = "Control-label col-md-2" })</span> 
     <div class="col-md-10"> 
      @Html.TextBoxFor(model => model.postedPicture, new {type 
     ="file" }) 
      @Html.ValidationMessage("customMessage") 
     </div> 
    </div> 

    <div class="form-group"> 
     <div class="col-md-offset-2 col-md-10 createbutton"> 
      <input type="submit" value="Create" class="btn btn-default" /> 
     </div> 
    </div> 
    } 

我每次更新數據庫或在瀏覽器中運行的代碼,它給我這個錯誤:

"Value cannot be null. Parameter name: entitySet".

這裏是調用堆棧:

 System.ArgumentNullException: Value cannot be null. 
Parameter name: entitySet 
    at System.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName) 
    at System.Data.Entity.Core.Mapping.EntitySetMapping..ctor(EntitySet entitySet, EntityContainerMapping containerMapping) 
    at System.Data.Entity.ModelConfiguration.Edm.DbDatabaseMappingExtensions.AddEntitySetMapping(DbDatabaseMapping databaseMapping, EntitySet entitySet) 
    at System.Data.Entity.ModelConfiguration.Edm.Services.TableMappingGenerator.Generate(EntityType entityType, DbDatabaseMapping databaseMapping) 
    at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.GenerateEntityTypes(DbDatabaseMapping databaseMapping) 
    at System.Data.Entity.ModelConfiguration.Edm.Services.DatabaseMappingGenerator.Generate(EdmModel conceptualModel) 
    at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo) 
    at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection) 
    at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext) 
    at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input) 
    at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() 
    at System.Data.Entity.Internal.LazyInternalContext.get_ModelBeingInitialized() 
    at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer) 
    at System.Data.Entity.Utilities.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w) 
    at System.Data.Entity.Utilities.DbContextExtensions.GetModel(Action`1 writeXml) 
    at System.Data.Entity.Utilities.DbContextExtensions.GetModel(DbContext context) 
    at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext, DatabaseExistenceState existenceState, Boolean calledByCreateDatabase) 
    at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration) 
    at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator() 
    at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run() 
    at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) 
    at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate) 
    at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner) 
    at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force) 
    at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0() 
    at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command) 

但是當我註釋掉類ExtendedNewsModel時,它更新數據庫並在瀏覽器中運行。

我想知道我做錯了什麼,或者如果有另一種方式只是使用HttpPostedFileBase而不將它放入類中以及如何將其渲染到視圖。

+0

發佈* full *異常,包括其調用堆棧以及拋出它的代碼。你只發布了兩個數據類,沒有任何可能拋出任何東西的方法。你可以簡單地通過調用Exception.ToString()來獲得完整的異常。 –

+0

你得到的錯誤不在你發佈的代碼中 – Jamiec

+0

順便說一句'HttpPostedFileBase'與數據庫無關。錯誤消息抱怨'entitySet'。發佈您的數據訪問代碼 –

回答

-1

我不認爲你可以使用HttpPostedFileBase這裏編碼,這個類只用於操縱使用Form Data方法接收的文件。

如果您試圖在視圖中顯示文件,您可以將其轉換爲base64字符串並將其作爲屬性與View Model一起發送(我通常不推薦此方法),或者最佳做法是顯示該文件(您的案例中的圖片)通過使用URL引用它。

相關問題