i am using view model to upload multiple videos but the videos are not uploading. before using the view model code working fines but now when i click on upload button and debug then the video object in HTTP post action of upload is null here is my code of controller
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);
}
[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;
// Video video = new Video();
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);
}
}
and the code of view is
@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>
}
and this is my viewmodel
public class VideoView:Video
{
public HttpPostedFileBase file { get; set; }
}