简体   繁体   中英

How to save image path to database? MVC.

I can upload image. But I´ve made it without saving path to database. How can I do this?

I removed a few functions on a view that the code was shorter. I hope that everything is understood.

Here what i got:

MODEL:

     public class Ogloszenie
    {
        [Key, ForeignKey("Pojazd")]
        public int PojazdOgloszenieId { get; set; }
        public RodzajPaliwa RodzajPaliwa { get; set; }
        public int RokProdukcji { get; set; }
        public int MocSilnika { get; set; }
        public int Przebieg { get; set; }
        public DateTime DataPrzegladu { get; set; }
        public DateTime DataUbezpieczenia { get; set; }
        public string OpisPojazdu { get; set; }

//path
        public string Zdjecie { get; set; }
//path

        public virtual Pojazd Pojazd { get; set; }
    }

CONTROLLER:

     [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "PojazdOgloszenieId,RodzajPaliwa,RokProdukcji,MocSilnika,Przebieg,DataPrzegladu,DataUbezpieczenia,OpisPojazdu,Zdjecie")] Ogloszenie ogloszenie, HttpPostedFileBase file)
            {
                if (ModelState.IsValid)
                {
                    if (file != null)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(Server.MapPath("~/Zdjecia/"), fileName);
                        file.SaveAs(path);

//*********************?????????? Something like this?     
Zdjecie = Url.Content("~/Zdjecia/" + file);
                    }

                    db.Ogloszenia.Add(ogloszenie);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

                ViewBag.PojazdOgloszenieId = new SelectList(db.Pojazdy, "ID", "Marka", ogloszenie.PojazdOgloszenieId);
                return View(ogloszenie);
            }

VIEW:

    @model AutoMonit.Models.Ogloszenie

    <h2>Utwórz ogłoszenie</h2>

    @using (Html.BeginForm("Create", "Ogloszenie", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()

        <div class="form-horizontal">
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.PojazdOgloszenieId, "PojazdOgloszenieId", htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.DropDownListFor(model => model.PojazdOgloszenieId, null, htmlAttributes: new { @class = "form-control" })
                    @Html.ValidationMessageFor(model => model.PojazdOgloszenieId, "", new { @class = "text-danger" })
                </div>
            </div>

***************
.
.
.
***************

    //FILE UPLOADING
                <label for="file">Filename:</label>
                <input type="file" name="file" id="file" />



            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Utwórz" class="btn btn-default" />
                </div>
            </div>
        </div>
    }

    <div>
        @Html.ActionLink("Wróć", "Index")
    </div>

Ok i made it (Thanks codeRecap for inspiration ;) )

    [HttpPost]
            [ValidateAntiForgeryToken]
            public ActionResult Create([Bind(Include = "PojazdOgloszenieId,RodzajPaliwa,RokProdukcji,MocSilnika,Przebieg,DataPrzegladu,DataUbezpieczenia,OpisPojazdu")] Ogloszenie ogloszenie, HttpPostedFileBase file)
            {
                if (ModelState.IsValid)
                {
                    if (file != null)
                    {
                        var fileName = Path.GetFileName(file.FileName);
                        var path = Path.Combine(Server.MapPath("~/Zdjecia/"), fileName);
                        file.SaveAs(path);

 ogloszenie.Zdjecie = Url.Content("~/Zdjecia/" + fileName);

                    }

                    db.Ogloszenia.Add(ogloszenie);
                    db.SaveChanges();
                    return RedirectToAction("Index");
                }

                ViewBag.PojazdOgloszenieId = new SelectList(db.Pojazdy, "ID", "Marka", ogloszenie.PojazdOgloszenieId);
                return View(ogloszenie);
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM