簡體   English   中英

如何將圖像路徑保存到數據庫? MVC。

[英]How to save image path to database? MVC.

我可以上傳圖片。 但是我做到了,卻沒有保存到數據庫的路徑。 我怎樣才能做到這一點?

鑒於代碼較短,我刪除了一些功能。 我希望一切都被理解。

這是我得到的:

模型:

     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; }
    }

控制器:

     [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);
            }

視圖:

    @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>

好的,我做到了(感謝codeRecap的啟發;))

    [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);
            }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM