【问题标题】:MVC 4 - Compiler Error Message: CS1061MVC 4 - 编译器错误消息:CS1061
【发布时间】:2016-06-30 23:48:49
【问题描述】:

我创建了一个图片库上传,它只适用于图片描述和上传图片。现在我正在添加一个下拉菜单,将图像分类到特定的组文件夹中。

我的数据库表如下:

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>

所以现在当我想查看创建页面时,我现在收到此错误:

“/”应用程序中的服务器错误。

编译错误

描述:编译资源时出错 需要为该请求提供服务。请查看以下具体内容 错误详细信息并适当地修改您的源代码。

编译器错误消息:CS1061:'T.Models.WebsitePhotosGallery.Photo' 不包含“GalleryCategory”的定义并且没有扩展名 方法“GalleryCategory”接受类型的第一个参数 'T.Models.WebsitePhotosGallery.Photo' 可以找到(你错过了吗? using 指令还是程序集引用?)

来源错误:

第 30 行: 第 31 行: 第 32 行:@Html.LabelFor(m => Model.GalleryCategory, 新的 { @class= "control-label col-sm-3", required = "" }) 第 33 行:
第 34 行:@Html.TextBoxFor(m => m.GalleryCategory, new { @class= "需要表单控件" })

源文件:c:\Users\Huha\Source\Workspaces\Panel\panel\Gallery Panel\Views\Gallery\Create.cshtml 行:32

非常感谢您的帮助,谢谢。

【问题讨论】:

  • 您的 POST 方法有一个参数 WebsitePhotosGallery photo,它与您在视图中使用的模型不同(它是 Photo,而不是 WebsitePhotosGallery),所以无论如何这一切都会失败。最好的猜测是您有多个名为Photo 的类,并且您引用了错误的类。在您的视图中拥有@using T.Database 也没有任何意义。

标签: asp.net-mvc asp.net-mvc-4


【解决方案1】:

您是否将解决方案拆分为多个项目?如果是这样,请尝试强制构建包含类Photo 的项目。听起来 WebApplication 只看到一个过时的 DLL,其中 Photo.GalleryCategory 属性尚不存在。

如果 Photo 项目的构建失败(或被跳过),则将使用最后构建的 DLL 版本。

【讨论】:

  • 你好彼得,解决方案是一个项目而不是多个项目。
  • 连同这个结论(以及 Huha 的 cmets 对我的回答),如果您要发布到外部服务器(可能是 IIS 实例?),请确保发布整个项目而不仅仅是视图。一切听起来好像您的模型没有被反映为具有新属性,因此 razor 发出错误(在视图的编译时)。
  • 您好,布拉德,感谢您的回复。好吧,您第一次提到我的应用程序看到了一个过时的 DLL。非常感谢。 :-)
【解决方案2】:

从这里看,您的视图和 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>

【讨论】:

  • 您好,布拉德,感谢您查看我的问题。我按照您的建议更改了代码,但错误仍然存​​在。我还尝试完全删除标签,只保留下拉菜单,但尝试访问下拉菜单时错误仍然存​​在。
  • 源错误:第 32 行:第 33 行:
    第 34 行:@Html.TextBoxFor(m => m.GalleryCategory, new { @class= "需要表单控件" }) 第 35 行:@Html.ValidationMessageFor(model => model.GalleryCategory) 第 36 行:
    源文件:c:\Users\Huha\Source\Workspaces\Panel\panel\Gallery Panel\ Views\Gallery\Create.cshtml 行:34
  • 如果我尝试引用 model.Description 或 model.ImagePath 代码工作正常。但是当我使用 model.GalleryCategory 时出现错误...
  • 更新错误:@Html.DropDownListFor(m => m.GalleryCategory, galleryCategories, "-- 选择图库类别 --", new { @class= "form-control", required = "" }) ......我注意到我在发送问题时忘记将文本框更改为下拉菜单。请帮助我,谢谢。
猜你喜欢
  • 1970-01-01
  • 2011-10-07
  • 2016-04-01
  • 2011-01-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多