我创建了一个图片库上传,只需使用图片描述和上传图片即可。现在我添加一个下拉列表,将图像分类到特定的组文件夹中。
我的数据库表格如下:
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();
}
}
}
最后我的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>
现在,当我想查看“创建”页面时,我现在收到此错误:
&#39; /&#39;中的服务器错误应用
编译错误
描述:编译资源期间发生错误 需要为此请求提供服务。请查看以下具体内容 错误详细信息并适当修改源代码。
编译器错误消息:CS1061:&#39; T.Models.WebsitePhotosGallery.Photo&#39; 不包含&#39; GalleryCategory&#39;的定义没有延伸 方法&#39; GalleryCategory&#39;接受第一个类型的参数 &#39; T.Models.WebsitePhotosGallery.Photo&#39;可以找到(你错过了吗? using指令或程序集引用?)
来源错误:
第30行:第31行: 第32行:@ Html.LabelFor(m =&gt; Model.GalleryCategory, new {@class =&#34; control-label col-sm-3&#34 ;, required =&#34;&#34;第33行:
第34行:@ Html.TextBoxFor(m =&GT; m.GalleryCategory,new {@class =&#34; form-control required&#34; })源文件:c:\ Users \ Huha \ Source \ Workspaces \ Panel \ panel \ Gallery Panel \ Views \ Gallery \ Create.cshtml Line:32
非常感谢您的帮助,谢谢。
答案 0 :(得分: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>
答案 1 :(得分:0)
您是否将解决方案拆分为多个项目?如果是这样,请尝试强制构建包含类Photo
的项目。听起来WebApplication只看到过时的DLL,其中Photo.GalleryCategory
属性尚不存在。
如果Photo
项目的构建失败(或被跳过),则将使用最后构建的DLL版本。