Just like the title say, I am building an Library. After many hours of googling and trial and error, I have finally accomplished the task of uploading image, saving to database, creating a thumbnail and showing in View. I am looking for uploading a second
file *PDF with a second input to another path
[Table("Document")]
public class Document
{
[Key]
public int IdDoc { get; set; }
public string Titre { get; set; }
public string Description { get; set; }
public virtual string Image { get; set; }
public virtual string Pdf { get; set; }
public virtual Type Type { get; set; }
}
[Table("Type")]
public class Type
{
[Key]
public int IdType { get; set; }
public string Label { get; set; }
public ICollection<Document> Documents { get; set; }
}
For now as i said, i only can upload the first input for picture, but not the second input for a pdf files and not the selected value in the dropdownlist ..
the browser only posts inputs with a name attribute. add a name to the pdf file input. your select value will posy to a string parameter named TypeList. You probably wanted to bind it to "Type" instead. use he DropDownListFor(m->m.Type, ViewBag.TypeList)
I already try the input with name attribute but doesn't work
Phrases like "doesn't work" are not specific enough to guess what does not work. Is the browser uploading the file but your code has a bug? You can check by opening Dev Tools (F12) and looking at the request.
A few things I noticed... you should not use the EF models in your View. Rather use View Models. The submit button is named "file" and there is an input parameter named file. There is no indication in the code where the action is handling multiple files.
There is no indication if you fixed the select name.
Is there anyway you can post the updated code so we can verify?
It's my first time with asp.net mvc ef6
It's the updated code, what doesn't work it's the upload of a second file to another path for the same document.. I don't know how to proceed.
The attribute name for the second input i removed intentionally because it upload the file twice to the same path..
The source code is the record of truth. We need the source code or code that reproduces the issue in order to provide assistance.
khalilbnzz
It's the updated code, what doesn't work it's the upload of a second file to another path for the same document.. I don't know how to proceed.
Post the most recent code as requested.
khalilbnzz
The attribute name for the second input i removed intentionally because it upload the file twice to the same path..
As stated above, browsers submit inputs that have names. inputs without names are not submitted. Guessing how technology works is not a good approach. The follow tutorial explains how to upload multiple files.
IMHO, you should use your EF6 models in MVC. EF6 models belong to EF not MVC. Rather craft a ViewModel and use the EF model to fill the ViewModel. This will help separate your data access layer from the UI plus it is easier to take advantage of model
validation. Maybe go through a few getting started tutorials.
According to your code,I suggest you could add ForeignKey in model,like:
[Table("Document")]
public class Document
{
[Key]
public int IdDoc { get; set; }
public string Titre { get; set; }
public string Description { get; set; }
public virtual string Image { get; set; }
public virtual string Pdf { get; set; }
public int IdType { get; set; }
[ForeignKey("IdType")]
public virtual Type Type { get; set; }
}
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
The INSERT statement conflicts with the FOREIGN KEY constraint "FK_dbo.Document_dbo.Type_IdType". The conflict occurred in the database "BabyDB", table "dbo.Type", column "IdType'.
The investigation has been stopped.
EDIT : After i edit the Create Controller, it add an other IdType that doesn't exit
The INSERT statement conflicts with the FOREIGN KEY constraint "FK_dbo.Document_dbo.Type_IdType". The conflict occurred in the database "BabyDB", table "dbo.Type", column "IdType'.
You added IdType = document.Type.IdType in your insert,you're right.
And the other question,you inserted 5 in IdType.
This record stores a null value.
I suggest you could delete this record which IdType is 5 in Type table.
It will be OK.
Best Regards.
Yuki Tao
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
3 Points
15 Posts
ASP.Net MVC 5 - Upload Image & PDF, Save to Database & Save to Database to selected value in my d...
Jan 15, 2019 02:24 PM|uid625849|LINK
Just like the title say, I am building an Library. After many hours of googling and trial and error, I have finally accomplished the task of uploading image, saving to database, creating a thumbnail and showing in View. I am looking for uploading a second file *PDF with a second input to another path
First the Create View
Then the Controller Create Action
And my two classes
For now as i said, i only can upload the first input for picture, but not the second input for a pdf files and not the selected value in the dropdownlist ..
Thanks
All-Star
57864 Points
15491 Posts
Re: ASP.Net MVC 5 - Upload Image & PDF, Save to Database & Save to Database to selected value in...
Jan 15, 2019 03:09 PM|bruce (sqlwork.com)|LINK
the browser only posts inputs with a name attribute. add a name to the pdf file input. your select value will posy to a string parameter named TypeList. You probably wanted to bind it to "Type" instead. use he DropDownListFor(m->m.Type, ViewBag.TypeList)
Member
3 Points
15 Posts
Re: ASP.Net MVC 5 - Upload Image & PDF, Save to Database & Save to Database to selected value in...
Jan 15, 2019 03:20 PM|uid625849|LINK
All-Star
52101 Points
23237 Posts
Re: ASP.Net MVC 5 - Upload Image & PDF, Save to Database & Save to Database to selected value in...
Jan 15, 2019 03:40 PM|mgebhard|LINK
Phrases like "doesn't work" are not specific enough to guess what does not work. Is the browser uploading the file but your code has a bug? You can check by opening Dev Tools (F12) and looking at the request.
A few things I noticed... you should not use the EF models in your View. Rather use View Models. The submit button is named "file" and there is an input parameter named file. There is no indication in the code where the action is handling multiple files. There is no indication if you fixed the select name.
Is there anyway you can post the updated code so we can verify?
Member
3 Points
15 Posts
Re: ASP.Net MVC 5 - Upload Image & PDF, Save to Database & Save to Database to selected value in...
Jan 15, 2019 04:21 PM|uid625849|LINK
It's the updated code, what doesn't work it's the upload of a second file to another path for the same document.. I don't know how to proceed.
The attribute name for the second input i removed intentionally because it upload the file twice to the same path..
All-Star
52101 Points
23237 Posts
Re: ASP.Net MVC 5 - Upload Image & PDF, Save to Database & Save to Database to selected value in...
Jan 15, 2019 05:11 PM|mgebhard|LINK
The source code is the record of truth. We need the source code or code that reproduces the issue in order to provide assistance.
Post the most recent code as requested.
As stated above, browsers submit inputs that have names. inputs without names are not submitted. Guessing how technology works is not a good approach. The follow tutorial explains how to upload multiple files.
https://www.mikesdotnetting.com/article/287/uploading-multiple-files-with-asp-net-mvc
IMHO, you should use your EF6 models in MVC. EF6 models belong to EF not MVC. Rather craft a ViewModel and use the EF model to fill the ViewModel. This will help separate your data access layer from the UI plus it is easier to take advantage of model validation. Maybe go through a few getting started tutorials.
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
Member
3 Points
15 Posts
Re: ASP.Net MVC 5 - Upload Image & PDF, Save to Database & Save to Database to selected value in...
Jan 15, 2019 07:23 PM|uid625849|LINK
The most recent code
The Controller Create :
The View Code :
After testing, it save two items
Contributor
3710 Points
1431 Posts
Re: ASP.Net MVC 5 - Upload Image & PDF, Save to Database & Save to Database to selected value in...
Jan 16, 2019 10:24 AM|Yuki Tao|LINK
Hi khalilbnzz,
According to your code,I suggest you could add ForeignKey in model,like:
[Table("Document")] public class Document { [Key] public int IdDoc { get; set; } public string Titre { get; set; } public string Description { get; set; } public virtual string Image { get; set; } public virtual string Pdf { get; set; } public int IdType { get; set; } [ForeignKey("IdType")] public virtual Type Type { get; set; } }
And modify the DropDownList to:
@Html.DropDownListFor(model => model.Type.IdType, (IEnumerable<SelectListItem>)ViewBag.TypeList, "Select Type", new { @class = "form-control" })
How it works in my project:
Best Regards.
Yuki Tao
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
3 Points
15 Posts
Re: ASP.Net MVC 5 - Upload Image & PDF, Save to Database & Save to Database to selected value in...
Jan 16, 2019 10:35 AM|uid625849|LINK
Thanks Yuki, i'll give it a try
The INSERT statement conflicts with the FOREIGN KEY constraint "FK_dbo.Document_dbo.Type_IdType". The conflict occurred in the database "BabyDB", table "dbo.Type", column "IdType'.
The investigation has been stopped.
EDIT : After i edit the Create Controller, it add an other IdType that doesn't exit
EDIT 2 : It's done like a charm, thank you
public ActionResult Create(Document document, HttpPostedFileBase file) { ViewBag.TypeList = new SelectList(db.Types, "IdType", "Label"); if (ModelState.IsValid) { string path = Path.Combine(Server.MapPath("~/Content/ImageUploaded/"), Path.GetFileName(file.FileName)); file.SaveAs(path); db.Documents.Add(new Document { IdDoc = document.IdDoc, Titre = document.Titre, Description = document.Description, Image = file.FileName, IdType = document.Type.IdType }); //db.Documents.Add(document); db.SaveChanges(); return RedirectToAction("Index"); } return View(document); }
Contributor
3710 Points
1431 Posts
Re: ASP.Net MVC 5 - Upload Image & PDF, Save to Database & Save to Database to selected value in...
Jan 17, 2019 05:52 AM|Yuki Tao|LINK
Hi khalilbnzz,
You added IdType = document.Type.IdType in your insert,you're right.
And the other question,you inserted 5 in IdType.
This record stores a null value.
I suggest you could delete this record which IdType is 5 in Type table.
It will be OK.
Best Regards.
Yuki Tao
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.