locked
Upload 2 individuals files in one form for the same Id (Document Class) RRS feed

  • Question

  • User-1917797656 posted

    <p>Hello,</p>
    <p>J'ai besoin de transférer 2 images dans 2 différents chemins mais pour le même document, et je ne sais pas comment y procéder vu que je viens de débuter en ASP.NET MVC &amp; EF6</p>
    <p>In the picture below my form</p>
    <p><img src="https://i.imgur.com/G3zwWm1.png" width="643" height="409" alt=""></p>
    <p>And this is the overview I need to get</p>
    <p><img src="https://i.imgur.com/VRXZWw7.png" alt="" width="621" height="429"></p>
    <p>The code of my class Document.cs</p>
    <pre class="prettyprint lang-auto">using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    namespace ApplicationTest.Models
    {
    [Table(&quot;Document&quot;)]
    public class Document
    {

    [Key]
    public int IdDoc { get; set; }
    public string Titre { get; set; }
    public string Description { get; set; }
    <span style="background-color:#ffff00"> public virtual string Image { get; set; }
    public virtual string Pdf { get; set; }</span>
    public int IdType { get; set; }
    [ForeignKey(&quot;IdType&quot;)]
    public virtual Type Type { get; set; }

    }
    }</pre>
    <p>The input part of my Create.cshtml view<br>
    <br>
    </p>
    <pre class="prettyprint lang-auto">&lt;div class=&quot;form-group&quot;&gt;
    @Html.LabelFor(model =&gt; model.Image, htmlAttributes: new { @class = &quot;control-label col-md-2&quot; })
    &lt;div class=&quot;col-md-10&quot;&gt;
    &lt;input type=&quot;file&quot; class=&quot;form-control&quot; name=&quot;file&quot; accept=&quot;image/png,image/jpg,image/jpeg&quot; /&gt;
    @Html.ValidationMessageFor(model =&gt; model.Image, &quot;&quot;, new { @class = &quot;text-danger&quot; })
    &lt;/div&gt;
    &lt;/div&gt;

    &lt;div class=&quot;form-group&quot;&gt;
    @Html.LabelFor(model =&gt; model.Pdf, htmlAttributes: new { @class = &quot;control-label col-md-2&quot; })
    &lt;div class=&quot;col-md-10&quot;&gt;
    &lt;input type=&quot;file&quot; class=&quot;form-control&quot; accept=&quot;.pdf&quot; /&gt;
    @Html.ValidationMessageFor(model =&gt; model.Pdf, &quot;&quot;, new { @class = &quot;text-danger&quot; })
    &lt;/div&gt;
    &lt;/div&gt;</pre>
    <p>And finally my DocumentController.cs controller</p>
    <pre class="prettyprint lang-auto"> [HttpPost]
    [ValidateAntiForgeryToken]
    Document document, HttpPostedFileBase file)
    public ActionResult Create(Document document, HttpPostedFileBase file)
    {
    if (ModelState.IsValid)
    {
    ViewBag.TypeList = new SelectList(db.Types, &quot;IdType&quot;, &quot;Label&quot;);
    string path = Path.Combine(Server.MapPath(&quot;~/Content/ImageUploaded/&quot;), 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(&quot;Index&quot;);
    }

    return View(document);
    }</pre>
    <p>Thank you</p>


    EDIT : SOLVED
    Thursday, January 17, 2019 1:20 PM

Answers

  • User-1917797656 posted
    Solved
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 18, 2019 2:41 AM