I am writing an MVC 5 web application to update blog posts. I want the user to be able to upload the video to the content folder and then save the file name as a string in the database. However, I seem to be missing one important thing.
I have a way to update messages that work, except for part of the video.
public ActionResult Update(int? id, string title, string body, DateTime dateTime, string tags, Video video)
{
if (!IsAdmin)
{
return RedirectToAction("Index");
}
var post = GetPost(id);
post.Title = title;
post.Body = body;
post.DateTime = dateTime;
post.Tags.Clear();
post.VideoFileName = UploadVideo(video);
I created a class for videos with one property.
public class Video
{
public HttpPostedFileBase File { get; set; }
}
Then the method in the same class as the method Updatefor loading the video and returning the file name.
[HttpPost]
public string UploadVideo(Video video)
{
if (video.File.ContentLength <= 0) return null;
var fileName = Path.GetFileName(video.File.FileName);
if (fileName == null) return null;
var path = Path.Combine(Server.MapPath("~/Content/Videos"), fileName);
video.File.SaveAs(path);
return fileName;
}
Then I have a View for Update method, but I donโt know how to get a video object from this view into the Update method to pass it to the method UploadVideo.
<form action="@Href("~/Posts/Update")" method="post" id="postForm">
@if(Model.Id != -1)
{
<input type="hidden" name="id" value="@Model.Id"/>
}
@{ var dateTime = Model.DateTime.Year > 2000 ? Model.DateTime : DateTime.Now; }
<input type="text" name="dateTime" value="@dateTime "/> Date<br />
<input type="text" name="title" value="@Model.Title " /> Title<br />
<input type="text" name="tags" value="@ViewBag.Tags " /> Tags<br />
<textarea name="body" rows="10" cols="80">@Model.Body</textarea><br />
<br/>
<br/>
<input type="file" name="video" />
<br/>
<br/>
<input type="submit" name="submit" value="Submit" />
</form>
<input type="file" name="video" /> , null Update.
Update , , dateTime, title, tags body?