Spent a lot of time going through similar articles in search for an answer to this question, here are some of them:
1Link, (Unable to show more than 2 links, but i have a bunch of them)
And most relevant 8 Link but that project is not working
But after going through this examples i still unable to sort it out. At the moment my project works without images, im able to CRUD new articles to page and remove it. But now i need to add image.
First of all, i need an image to be as part of a class (i'm creating a new article with some title, short description, text body and image) here is model i came up with, this is non working code. As i want to store link to file to db so i guess i need to create new variable ImagePath
namespace PhClub.Models
{
public class Media
{
public int Id { get; set; }
public string title { get; set; }
public string description { get; set; }
[Required, StringLength(2048), DataType(DataType.MultilineText)]
public string body { get; set; }
public string ImagePath { get; set; }
}
}
Next i need to add create form to get a filename to upload it. Here is Create view code snippet
@using (Html.BeginForm("Create", "Medias", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<hr />
@Html.ValidationSummary(true)
<div class="form-group">
@Html.LabelFor(model => model.title, "Title", new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.title)
@Html.ValidationMessageFor(model => model.title)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.description, "Description",new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.description)
@Html.ValidationMessageFor(model => model.description)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.body, "Media",new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.body)
@Html.ValidationMessageFor(model => model.body)
</div>
</div>
**<div class="form-group">
File name
<div class="uploadfile">
@Html.TextBoxFor(model => model.file, new { type = "file" })
@Html.ValidationMessageFor(model => model.file)
<input type="submit" value="Submit" />
</div>
</div>**
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Создать" class="btn btn-default" />
</div>
</div>
</div>
}
However here i have two uncertainties: 1. I already have a Create method in in Controller, it was generated by EF for me:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "Id,title,description,body")] Media media)
{
if (ModelState.IsValid)
{
db.Medias.Add(media);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(media);
}
But i need to integrate it somehow with this method somehow (i took this one from one of the examples)
[HttpPost]
public ActionResult FileUpload(Media mRegister)
{
//Check server side validation using data annotation
if (ModelState.IsValid)
{
//TO:DO
var fileName = Path.GetFileName(mRegister.file.FileName);
var path = Path.Combine(Server.MapPath("~/Content/Upload"), fileName);
mRegister.file.SaveAs(path);
ViewBag.Message = "File has been uploaded successfully";
ModelState.Clear();
}
return View();
}
I cannot have two methods with the same name - CREATE doing different things on one hand and on other I cannot give different name to it as i don't need other View for it, it needs to be shown on Create view if im understanding what is going on here. 2. Secondary - do i need to have two buttons for this? one for upload image and one for creating object?
And finally i need to show image as part of the article somehow in my Media view. At the moment it looks like that
<h2>@Model.title</h2>
<br>
<div class="media">
<p>
<strong>@Html.Raw(Model.title)</strong>
</p>
<p>
@Html.Raw(Model.description)
</p>
<p>
@Html.Raw(Model.body)
</p>
</div>
<div style ="float:right margin: 0px; 0px; 15px; 15px;">
@Html.Raw(Model.file)
</div>
From here I'm lost. I need to place that image in certain place, but don't know how.In other examples people creating bunch of other classes for file. Do i need it? What the easiest way of doing it? I know I'm not the first who came up with this question, but i could really use your help guys.