Asp.net mvc MVC4-编译器错误消息:CS1061
我已经创建了一个图像库上传,只需图像描述和上传图像就可以了。现在,我添加了一个下拉列表,将图像分类到特定的组文件夹中 我的数据库表如下:Asp.net mvc MVC4-编译器错误消息:CS1061,asp.net-mvc,asp.net-mvc-4,Asp.net Mvc,Asp.net Mvc 4,我已经创建了一个图像库上传,只需图像描述和上传图像就可以了。现在,我添加了一个下拉列表,将图像分类到特定的组文件夹中 我的数据库表如下: CREATE TABLE [WebsitePhotosGallery] ( [PhotoId] UNIQUEIDENTIFIER DEFAULT (newid()) NOT NULL, [Decription] NVARCHAR (150) NOT NULL, [ImagePath] NVARC
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)notnull
我的数据库模型如下所示:
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();
}
}
}
我的控制器如下所示:
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();
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统图;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
利用T.数据库;
使用T.Models.WebsitePhotosGallery;
名称空间T.控制器
{
公共类GalleryController:控制器
{
//
//获取照片库/
DatabaseEntity db=新建DatabaseEntity();
公共操作结果索引(字符串筛选器=null,int page=1,int pageSize=18)
{
var records=new PagedList();
ViewBag.filter=过滤器;
records.Content=db.WebsitePhotosGalleries.Where(x=>filter==null | | |(x.description.Contains(filter)))
.OrderByDescending(x=>x.description)
.Skip((第1页)*页面大小)
.Take(页面大小)
.ToList();
//计数
records.TotalRecords=db.WebsitePhotosGalleries.Where(x=>filter==null | | |(x.description.Contains(filter))).Count();
records.CurrentPage=第页;
records.PageSize=页面大小;
返回视图(记录);
}
[HttpGet]
公共操作结果创建()
{
var photo=新照片();
返回视图(照片);
}
公共大小NewImageSize(大小imageSize,大小newSize)
{
规模最终化;
双时差;
if(imageSize.Height>newSize.Height | | imageSize.Width>newSize.Width)
{
如果(imageSize.Height>imageSize.Width)
tempval=newSize.Height/(imageSize.Height*1.0);
其他的
tempval=newSize.Width/(imageSize.Width*1.0);
最终尺寸=新尺寸((int)(tempval*imageSize.Width),(int)(tempval*imageSize.Height));
}
其他的
finalSize=imageSize;//图像已经很小了
返回最终化;
}
私有void SaveToFolder(图像img、字符串文件名、字符串扩展名、大小新闻大小、字符串路径保存)
{
//获得新的解决方案
Size imgSize=NewImageSize(img.Size,newSize);
使用(System.Drawing.Image newImg=新位图(img,imgSize.Width,imgSize.Height))
{
newImg.Save(Server.MapPath(pathToSave),img.RawFormat);
}
}
[HttpPost]
公共操作结果创建(网站照片库照片,IEnumerable文件)
{
如果(!ModelState.IsValid)
返回视图(照片);
if(files.Count()==0 | | files.FirstOrDefault()==null)
{
ViewBag.error=“请选择一个文件”;
返回视图(照片);
}
var model=新网站PhotosGallery();
foreach(文件中的var文件)
{
如果(file.ContentLength==0)继续;
型号说明=照片说明;
var fileName=Guid.NewGuid().ToString();
var s=System.IO.Path.GetExtension(file.FileName);
如果(s!=null)
{
var扩展=s.ToLower();
使用(var img=System.Drawing.Image.FromStream(file.InputStream))
{
model.ThumbPath=String.Format(“/galleryimage/Thumbs/{0}{1}”,文件名,扩展名);
model.ImagePath=String.Format(“/GalleryImage/{0}{1}”,文件名,扩展名);
//保存缩略图大小的图像,240 x 159
SaveToFolder(img、文件名、扩展名、新大小(240159)、model.ThumbPath);
//保存大尺寸图像,1024 x 683
SaveToFolder(img、文件名、扩展名、新大小(1024683)、model.ImagePath);
}
}
//将记录保存到数据库
model.CreatedOn=DateTime.Now;
model.PhotoId=Guid.NewGuid();
model.GalleryCategory=photo.GalleryCategory;
添加(模型);
db.SaveChanges();
}
返回视图();
}
}
}
最后,我的观点是这样的,这就是现在出现错误的地方,我硬编码了下拉列表:
@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>
@使用T.数据库
@模型T.WebsitePhotosGallery.Photo
@{
var galleryCategories=新列表
{
新建SelectListItem{Text=“Group 1”,Value=“Group 1”},
新建SelectListItem{Text=“Group 2”,Value=“Group 2”}
};
}
创造
上传图像
@使用(Html.BeginForm(“Create”、“Gallery”、FormMethod.Post、new{id=“photogallery”、enctype=“multipart/formdata”}))
{
@Html.AntiForgeryToken()
@Html.LabelFor(m=>Model.description,新的{@class=“控制标签col-sm-3”,必选=”“})
@TextBoxFor(m=>m.description,新的{@class=“form control required”})
@Html.ValidationMessageFor(model=>model.description)
@Html.LabelFor(m=>Model.GalleryCategory,新的{@class=“控制标签col-sm-3”,必需=”“})
@Html.DropDownListFor(m=>m.Product,productCategory,“--Select Product--”,new{@class=“form control”,required=”“})
@Html.ValidationMessage