私は次のフォームに取り組んでいます
@using (Html.BeginForm("BusinessMedia", "SmallBusiness", FormMethod.Post, new { enctype = "multipart/form-data" })){
@Html.HiddenFor(m => m.Business.BusinessID)
<div style="padding-right:850px">
@Html.Label(" ", "First Picture:")
<input type="file" name="BusinessPictures" id="FirstPicture" />
@Html.Label(" ", "Second Picture:")
<input type="file" name="BusinessPictures" id="SecondPicture" />
@Html.Label(" ", "Third Picture:")
<input type="file" name="BusinessPictures" id="ThirdPicture" />
@Html.Label(" ", "Fourth Picture:")
<input type="file" name="BusinessPictures" id="FourthPicture" />
@Html.Label(" ", "Fifth Picture:")
<input type="file" name="BusinessPictures" id="FifthPicture" />
@Html.Label(" ", "Video:")
<input type="file" name="BusinessVideo" id="Video" />
<div style="padding-left:40%; padding-bottom:63px;">
<p>
<input type="submit" class="btn btn-info" value="Upload Media" />
</p>
</div>
</div>
}
フォームに隠されているBusinessIDがコントローラーのモデルに届かない理由を理解できないようです。ファイルは問題なく通過します。これがコントローラーのアクションです。
[HttpPost]
public ActionResult BusinessMedia(BusinessModel model, IEnumerable<HttpPostedFileBase> BusinessPictures, HttpPostedFileBase BusinessVideo)
{
string VideoServerPath = Server.MapPath("~/videos/");
string ImageServerPath = Server.MapPath("~/images/");
if (model.SaveBusinessMedia(model, BusinessPictures, BusinessVideo, VideoServerPath, ImageServerPath))
return PartialView("PostRentalSuccess", model);
return PartialView("PostNewRental");
}
どんなアイデアでも大歓迎です。
ビジネスモデル追加
public class BusinessModel
{
public List<Business> RequestedBusinesses;
public BusinessHour RequestedBusinessHours;
public User User;
public RentalContext _rc;
public Business Business;
public List<String> PictureLinks;
public BusinessModel()
{
RequestedBusinesses = new List<Business>();
RequestedBusinessHours = new BusinessHour();
User = new User();
_rc = new RentalContext();
Business = new Business();
PictureLinks = new List<string>();
}
public void LoadUser(int UserID)
{
User = (User)_rc.Users.Find(UserID);
}
public void ReturnBusinesses(int ServiceTypeID)
{
var BusinessesRequested = from b in _rc.Businesses
where b.ServiceTypeID == ServiceTypeID
select b;
RequestedBusinesses = new List<Business>();
foreach (var item in BusinessesRequested)
{
RequestedBusinesses.Add(item);
}
}
public static Image ResizeImage(Image image, Size size)
{
int newWidth = size.Width;
int newHeight = size.Height;
Image newImage = new Bitmap(newWidth, newHeight);
using (Graphics graphicsHandle = Graphics.FromImage(newImage))
{
graphicsHandle.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphicsHandle.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}
public void PicturesForBusiness(int BusinessID)
{
var Pictures = from p in _rc.Pictures
where p.BusinessID.Value == BusinessID
select new { p.PictureLink };
if (Pictures.Count() > 0)
foreach (var item in Pictures)
PictureLinks.Add(item.PictureLink);
}
public void LoadBusiness(int BusinessID)
{
Business = (Business)_rc.Businesses.Find(BusinessID);
PicturesForBusiness(Business.BusinessID);
ReturnBusinessHours(Business.BusinessID);
}
public void ReturnBusinessHours(int BusinessID)
{
var BusinessHoursRequested = from bh in _rc.BusinessHours
where bh.BusinessID == BusinessID
select bh;
if (BusinessHoursRequested.Count() > 0)
foreach (var item in BusinessHoursRequested)
RequestedBusinessHours = item;
}
public bool SaveBusinessMedia(BusinessModel model, IEnumerable<HttpPostedFileBase> BusinessPictures, HttpPostedFileBase BusinessVideo, string VideoServerPath, string ImageServerPath)
{
int recordCount = 0;
Image Resized;
FileStream SmallPicture;
model.LoadBusiness(model.Business.BusinessID);
Business BusinessUser = (Business)from b in _rc.Businesses
where b.BusinessID == model.Business.BusinessID
select b;
model.LoadUser(BusinessUser.UserID);
int result = 0;
try
{
if (BusinessPictures != null)
{
foreach (HttpPostedFileBase requestFile in BusinessPictures)
{
if (requestFile.ContentLength > 0)
{
string fileName = Path.GetFileName(requestFile.FileName);
if (!Directory.Exists(ImageServerPath))
{
Directory.CreateDirectory(ImageServerPath);
}
string path = Path.Combine(ImageServerPath, fileName);
requestFile.SaveAs(path);
Picture Picture = new Picture();
Picture.BusinessID = model.Business.BusinessID;
Picture.Description = model.Business.Name;
Picture.PictureLink = "/images/" + fileName;
Picture.UploadedBy = model.User.UserID;
Picture.UploadedOn = DateTime.Now;
_rc.Pictures.Add(Picture);
recordCount++;
Resized = ResizeImage(Image.FromFile(path), new Size(75, 75));
string fileExt = fileName.Substring(fileName.IndexOf('.'), fileName.Count() - fileName.IndexOf('.'));
fileName.Remove(fileName.IndexOf('.'), fileName.Count() - fileName.IndexOf('.'));
path = Path.Combine(ImageServerPath, fileName + "_s" + fileExt);
Resized.Save(path, ImageFormat.Jpeg);
Picture.PictureLink = "/images/" + fileName;
_rc.Pictures.Add(Picture);
recordCount++;
}
}
}
if (BusinessVideo.ContentLength > 0)
{
string fileName = Path.GetFileName(BusinessVideo.FileName);
if (!Directory.Exists(VideoServerPath))
{
Directory.CreateDirectory(VideoServerPath);
}
string path = Path.Combine(VideoServerPath, fileName);
BusinessVideo.SaveAs(path);
Video Video = new Video();
Video.BusinessID = model.Business.BusinessID;
Video.Description = model.Business.Name;
Video.VideoLink = "/videos/" + fileName;
Video.UploadedBy = model.User.UserID;
Video.UploadedOn = DateTime.Now;
_rc.Videos.Add(Video);
recordCount++;
}
result = _rc.SaveChanges();
if (result == recordCount)
return true;
else
return false;
}
catch (DbEntityValidationException e)
{
foreach (var eve in e.EntityValidationErrors)
{
Console.WriteLine("Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
eve.Entry.Entity.GetType().Name, eve.Entry.State);
foreach (var ve in eve.ValidationErrors)
{
Console.WriteLine("- Property: \"{0}\", Error: \"{1}\"",
ve.PropertyName, ve.ErrorMessage);
}
}
throw;
}
catch (DbUpdateException e)
{
Console.WriteLine(e.Message);
Console.WriteLine(e.Data);
throw;
}
}
}
}