i am using view model to upload multiple videos but the videos are not uploading. before using the view model code working fine but now when i click on upload button and debug then the video object is null here is my code of controller
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Final.Models;
using System.Net;
using System.Data.Entity;
using System.IO;
namespace Final.Controllers
{
public class HomeController : Controller
{
private DataContext db = new DataContext();
public ActionResult Index()
{
return View();
}
public ActionResult About()
{
ViewBag.Message = "Your application description page.";
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
public ActionResult UploadedVideos()
{
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
var q = db.Videos.Where(v => v.UserId == user.Id).ToList();
return View(q);
}
public ActionResult Edit(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
Video video = db.Videos.Find(id);
if (video == null)
{
return HttpNotFound();
}
return View(video);
}
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit(Video video)
{
if (ModelState.IsValid)
{
Video dbvideo = db.Videos.Find(video.Id);
dbvideo.Tags = video.Tags;
dbvideo.Title = video.Title;
dbvideo.Description = video.Description;
dbvideo.Category = video.Category;
db.Entry(dbvideo).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("UploadedVideos");
}
return View(video);
}
[HttpPost]
public JsonResult DeleteFile(string id)
{
if (String.IsNullOrEmpty(id))
{
Response.StatusCode = (int)HttpStatusCode.BadRequest;
return Json(new { Result = "Error" });
}
try
{
Video fileDetail = db.Videos.Find(Convert.ToInt32(id));
if (fileDetail == null)
{
Response.StatusCode = (int)HttpStatusCode.NotFound;
return Json(new { Result = "Error" });
}
HttpFileCollectionBase files = Request.Files;
var path = Path.Combine(fileDetail.Path);
if (System.IO.File.Exists(path))
{
System.IO.File.Delete(path);
}
db.Videos.Remove(fileDetail);
db.SaveChanges();
return Json(new { Result = "OK" });
}
catch (Exception ex)
{
return Json(new { Result = "ERROR", Message = ex.Message });
}
}
public ActionResult upload() {
return View();
}
[HttpPost]
public ActionResult Upload(List<Models.ViewModels.VideoView> videos)
{
try
{
ApplicationUser user = System.Web.HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>().FindById(System.Web.HttpContext.Current.User.Identity.GetUserId());
HttpFileCollectionBase files = Request.Files;
foreach (var video in videos)
{
long time = DateTime.Now.Ticks;
string path = Server.MapPath("~/Images/" + time + "_" + video.file.FileName);
video.file.SaveAs(path);
video.Path = path;
video.DateTimeStamp = DateTime.Now;
video.UserId = user.Id;
db.Videos.Add(video);
}
db.SaveChanges();
return RedirectToAction("UploadedVideos");
}
catch(Exception e)
{
return Json("Failed Error : "+e.Message, JsonRequestBehavior.AllowGet);
}
}
}
}
What I have tried:
and the code of view is below
@model Final.Models.ViewModels.VideoView
@{
ViewBag.Title = "upload";
}
<h2>Index</h2>
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control inputField" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Category, new { htmlAttributes = new { @class = "form-control inputField" } })
@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Tags, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tags, new { htmlAttributes = new { @class = "form-control inputField" } })
@Html.ValidationMessageFor(model => model.Tags, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PrivacyStatus, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PrivacyStatus, new { htmlAttributes = new { @class = "form-control inputField" } })
@Html.ValidationMessageFor(model => model.PrivacyStatus, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Description, new { @cols = "50", @rows = "5" })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-horizontal">
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Title, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Title, new { htmlAttributes = new { @class = "form-control inputField" } })
@Html.ValidationMessageFor(model => model.Title, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Category, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Category, new { htmlAttributes = new { @class = "form-control inputField" } })
@Html.ValidationMessageFor(model => model.Category, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Tags, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tags, new { htmlAttributes = new { @class = "form-control inputField" } })
@Html.ValidationMessageFor(model => model.Tags, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.PrivacyStatus, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.PrivacyStatus, new { htmlAttributes = new { @class = "form-control inputField" } })
@Html.ValidationMessageFor(model => model.PrivacyStatus, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.TextAreaFor(model => model.Description, new { @cols = "50", @rows = "5" })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
</div>
<div class="form-group">
<input type="file" name="file" multiple id="file" /><br />
<div id="all-progress-bars"></div>
<input type="submit" value="Save and Upload" href="javascript:void(0)" id="bb" class="btn btn-default" />
<span id="display"></span>
</div>
</div>
</div>
}
the code of View Model is
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace Final.Models.ViewModels
{
public class VideoView:Video
{
public HttpPostedFileBase file { get; set; }
}
}