Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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()
        {
            //var SysUser = db.AspNetUsers.FirstOrDefault(u => u.UserID);
            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" });
                }

              
               

                //Delete file from the file system
                HttpFileCollectionBase files = Request.Files;
                    var path = Path.Combine(fileDetail.Path);
                    if (System.IO.File.Exists(path))
                    {
                        System.IO.File.Delete(path);
                    }
                //Remove from database
                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;
             
                //  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);
                
            }
        }
    }
}


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; }

    }
}
Posted
Updated 16-Mar-16 1:50am
v2
Comments
John C Rayan 16-Mar-16 7:52am    
Can you post the full view code. Also which action are you calling to upload the videos using the ViewModel?
arslan afzal bhatti 16-Mar-16 11:57am    
i am calling post function of upload and the code of view is listed in next comment
arslan afzal bhatti 16-Mar-16 11:58am    
here is full code of view of upload @John C Rayan

@model Final.Models.ViewModels.VideoView
@{
ViewBag.Title = "upload";
}

Index


@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 { @
arslan afzal bhatti 16-Mar-16 12:00pm    
@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>
}
John C Rayan 16-Mar-16 15:49pm    
In your action method , you have

HttpFileCollectionBase files = Request.Files;

You have to go through the files and you will find the files. Your viewmodel will not be mapped to files by default.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900