2016-03-15 139 views
0

我創建了一個圖片庫上傳,只需使用圖片描述和上傳圖片即可正常工作。現在我添加一個下拉菜單將圖像分類到特定的組文件夾中。MVC 4 - 編譯器錯誤消息:CS1061

我的數據庫表如下:

CREATE TABLE [WebsitePhotosGallery] (
    [PhotoId]   UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL, 
    [Decription]  NVARCHAR (150) NOT NULL, 
    [ImagePath]  NVARCHAR (200) NOT NULL, 
    [ThumbPath]  NVARCHAR (200) NOT NULL, 
    [CreatedOn]  DATETIME   NOT NULL, 
    [GalleryCategory] NVARCHAR (50) NOT NULL, 
    PRIMARY KEY CLUSTERED ([PhotoId] ASC) 
); 

注:我添加[GalleryCategory] NVARCHAR (50) NOT NULL,因爲現在下來所需的下降。

我的數據庫模型是這樣的:

namespace T.Database 
{ 
    using System; 
    using System.Collections.Generic; 

    public partial class WebsitePhotosGallery 
    { 
     public System.Guid PhotoId { get; set; } 
     public string Decription { get; set; } 
     public string ImagePath { get; set; } 
     public string ThumbPath { get; set; } 
     public System.DateTime CreatedOn { get; set; } 
     public string GalleryCategory { get; set; } 
    } 
} 

I also have this model 

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

namespace T.WebsitePhotosGallery 
{ 
    public class Photo 
    { 
     [Key] 
     public int PhotoId { get; set; } 

     [Display(Name = "Decription")] 
     [Required] 
     public String Decription { get; set; } 

     [Display(Name = "Image Path")] 
     public String ImagePath { get; set; } 

     [Display(Name = "Thumb Path")] 
     public String ThumbPath { get; set; } 


     [Display(Name = "Created On")] 
     public DateTime CreatedOn { get; set; } 

     [Display(Name = "Gallery Category")] 
     [Required] 
     public String GalleryCategory { get; set; } 

    } 
} 

我的控制器看起來是這樣的:

using System; 
using System.Collections.Generic; 
using System.Drawing; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using T.Database; 
using T.Models.WebsitePhotosGallery; 

namespace T.Controllers 
{ 
    public class GalleryController : Controller 
    { 
     // 
     // GET: /PhotosGallery/ 
     DatabaseEntity db = new DatabaseEntity(); 
     public ActionResult Index(string filter = null, int page = 1, int pageSize = 18) 
     { 
      var records = new PagedList<WebsitePhotosGallery>(); 
      ViewBag.filter = filter; 

      records.Content = db.WebsitePhotosGalleries.Where(x => filter == null || (x.Decription.Contains(filter))) 
        .OrderByDescending(x => x.Decription) 
        .Skip((page - 1)*pageSize) 
        .Take(pageSize) 
        .ToList(); 

      //Count 
      records.TotalRecords = db.WebsitePhotosGalleries.Where(x => filter == null || (x.Decription.Contains(filter))).Count(); 

      records.CurrentPage = page; 
      records.PageSize = pageSize; 

      return View(records); 

     } 

     [HttpGet] 
     public ActionResult Create() 
     { 
      var photo = new Photo(); 
      return View(photo); 
     } 
     public Size NewImageSize(Size imageSize, Size newSize) 
     { 
      Size finalSize; 
      double tempval; 
      if (imageSize.Height > newSize.Height || imageSize.Width > newSize.Width) 
      { 
       if (imageSize.Height > imageSize.Width) 
        tempval = newSize.Height/(imageSize.Height * 1.0); 
       else 
        tempval = newSize.Width/(imageSize.Width * 1.0); 

       finalSize = new Size((int)(tempval * imageSize.Width), (int)(tempval * imageSize.Height)); 
      } 
      else 
       finalSize = imageSize; //image is already small size 

      return finalSize; 
     } 

     private void SaveToFolder(Image img, string fileName, string extension, Size newSize, string pathToSave) 
     { 
      //Get new resolution 
      Size imgSize = NewImageSize(img.Size, newSize); 
      using (System.Drawing.Image newImg = new Bitmap(img, imgSize.Width, imgSize.Height)) 
      { 
       newImg.Save(Server.MapPath(pathToSave), img.RawFormat); 

      } 
     } 

     [HttpPost] 
     public ActionResult Create(WebsitePhotosGallery photo, IEnumerable<HttpPostedFileBase> files) 
     { 
      if (!ModelState.IsValid) 
       return View(photo); 
      if (files.Count() == 0 || files.FirstOrDefault() == null) 
      { 
       ViewBag.error = "Please choose a file"; 
       return View(photo); 
      } 

      var model = new WebsitePhotosGallery(); 
      foreach (var file in files) 
      { 
       if (file.ContentLength == 0) continue; 

       model.Decription = photo.Decription; 
       var fileName = Guid.NewGuid().ToString(); 
       var s = System.IO.Path.GetExtension(file.FileName); 
       if (s != null) 
       { 
        var extension = s.ToLower(); 

        using (var img = System.Drawing.Image.FromStream(file.InputStream)) 
        { 
         model.ThumbPath = String.Format("/GalleryImages/Thumbs/{0}{1}", fileName, extension); 
         model.ImagePath = String.Format("/GalleryImages/{0}{1}", fileName, extension); 

         //Save thumbnail size image, 240 x 159 
         SaveToFolder(img, fileName, extension, new Size(240, 159), model.ThumbPath); 

         //Save large size image, 1024 x 683 
         SaveToFolder(img, fileName, extension, new Size(1024, 683), model.ImagePath); 
        } 
       } 

       //Save record to database 
       model.CreatedOn = DateTime.Now; 
       model.PhotoId= Guid.NewGuid(); 
       model.GalleryCategory = photo.GalleryCategory; 
       db.WebsitePhotosGalleries.Add(model); 
       db.SaveChanges(); 
      } 

      return View(); 
     } 

    } 
} 

最後我查看看起來像這樣這就是現在的錯誤出現時,我硬編碼下拉列表:

@using T.Database 
@model T.WebsitePhotosGallery.Photo 

@{ 
    var galleryCategories = new List<SelectListItem> 
    { 
     new SelectListItem {Text = "Group 1", Value = "Group 1"}, 
     new SelectListItem {Text = "Group 2", Value = "Group 2"} 
    }; 
} 


<h2>Create</h2> 

<h2>Upload Images</h2> 
<div class="well"> 

     @using (Html.BeginForm("Create", "Gallery", FormMethod.Post, new { id = "photogallery", enctype = "multipart/form-data" })) 
     { 
      @Html.AntiForgeryToken() 

      <div class="form-horizontal"> 

       <div class="form-group"> 
        @Html.LabelFor(m => Model.Decription, new { @class = "control-label col-sm-3", required = ""}) 
        <div class="col-sm-5"> 
         @Html.TextBoxFor(m => m.Decription, new { @class = "form-control required" }) 
         @Html.ValidationMessageFor(model => model.Decription) 
        </div> 
       </div> 

       <div class="form-group"> 
        @Html.LabelFor(m => Model.GalleryCategory, new { @class = "control-label col-sm-3", required = "" }) 
        <div class="col-sm-5"> 
         @Html.DropDownListFor(m => m.Product, productCategory, "-- Select Product --", new {@class = "form-control", required = ""}) 
         @Html.ValidationMessageFor(model => model.GalleryCategory) 
        </div> 
       </div> 

       <div class="form-group"> 
        @Html.Label("Choose Image(s)", new { @class = "control-label col-sm-3", required = "" }) 
        <div class="col-sm-5"> 
         <input type="file" name="files" multiple="multiple" accept=".jpg, .png, .gif" required /> 
        </div> 
       </div> 

       <div class="form-group"> 
        <div class="col-sm-5 col-sm-offset-3"> 
         <input type="submit" value="Save" class="btn btn-primary" /> 
         <div style="color:red"> 
          @ViewBag.error 
         </div> 
        </div> 
       </div> 
      </div> 
     } 
    </div> 

所以現在立即當我想查看創建頁面我現在gett此錯誤:

Server Error in '/' Application.

Compilation Error

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1061: 'T.Models.WebsitePhotosGallery.Photo' does not contain a definition for 'GalleryCategory' and no extension method 'GalleryCategory' accepting a first argument of type 'T.Models.WebsitePhotosGallery.Photo' could be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 30: Line 31: Line 32: @Html.LabelFor(m => Model.GalleryCategory, new { @class = "control-label col-sm-3", required = "" }) Line 33:
Line 34: @Html.TextBoxFor(m => m.GalleryCategory, new { @class = "form-control required" })

Source File: c:\Users\Huha\Source\Workspaces\Panel\panel\Gallery Panel\Views\Gallery\Create.cshtml Line: 32

您的幫助非常感謝,謝謝。

+0

你的POST方法有一個參數'WebsitePhotosGallery photo',它是一個不同於你在視圖中使用的模型(它是'Photo',而不是'WebsitePhotosGallery'),所以它會導致所有的失敗。最好的猜測是你有多個名爲'Photo'的類,並且你引用了錯誤的類。在你的視圖中使用'@using T.Database'也沒有意義。 –

回答

0

你有沒有分裂溶液倒入多個項目?如果是這樣,請嘗試強制構建包含類Photo的項目。這聽起來像WebApplication只看到一個過時的DLL,其中Photo.GalleryCategory屬性尚不存在。

如果構建失敗(或跳過)Photo項目,則將使用最後生成的DLL版本。

+0

Hello Peter,解決方案是一個項目不是多個項目。 – Huha

+0

除了這個結論(以及Huha對我的回答的評論),如果你要發佈到外部服務器(可能是IIS實例?),確保你發佈整個項目而不僅僅是視圖。一切聽起來像你的模型沒有被反映爲一個新的屬性,因此剃刀發出錯誤(在視圖的編譯時)。 –

+0

你好布拉德,謝謝你的迴應。那麼你在第一次提到我的應用程序看到過時的DLL的地方是正確的。非常感謝你。 :-) – Huha

0

從這裏,它看起來像你的看法和htmlhelper有點關閉。你應該通過lambda引用該屬性,而不是通過Model。基本上,你認爲從雲:

<div class="form-group"> 
    @Html.LabelFor(m => Model.GalleryCategory, new { @class = "control-label col-sm-3", required = "" }) 
    <div class="col-sm-5"> 
     @Html.TextBoxFor(m => m.GalleryCategory, new { @class = "form-control required" }) 
     @Html.ValidationMessageFor(model => model.GalleryCategory) 
    </div> 
</div> 

以下(注變化@Html.LabelFor(...)調用):

<div class="form-group"> 
    @* Reference m. not Model. *@ 
    @Html.LabelFor(m => m.GalleryCategory, new { @class = "control-label col-sm-3", required = "" }) 
    <div class="col-sm-5"> 
     @Html.TextBoxFor(m => m.GalleryCategory, new { @class = "form-control required" }) 
     @Html.ValidationMessageFor(model => model.GalleryCategory) 
    </div> 
</div> 
+0

你好布拉德,謝謝你看我的問題。我按照您的建議更改了代碼,但錯誤仍然存​​在。我也嘗試徹底刪除標籤,只保留在下拉菜單中,但嘗試訪問下拉菜單時錯誤仍然存​​在。 – Huha

+0

源錯誤: 第32行: 線33:

Line 34: @Html.TextBoxFor(m => m.GalleryCategory, new { @class = "form-control required" }) Line 35: @Html.ValidationMessageFor(model => model.GalleryCategory) Line 36:
源文件:C:\用戶\呼哈\源\工作區\面板\面板\廊面板\視圖\廊\ Create.cshtml行:34 – Huha

+0

如果我嘗試引用model.Description或model.ImagePath的代碼工作正常。但是當我使用model.GalleryCategory然後我得到的錯誤...當我更新模型我跟着同樣的步驟,當我創建模型,但我得到這個錯誤 – Huha