如何将图像路径保存到数据库? MVC。

时间:2015-11-06 11:42:17

标签: c# asp.net asp.net-mvc

我可以上传图片。但是我没有保存数据库的路径。我怎么能这样做?

我删除了代码较短的视图上的一些函数。我希望一切都能被理解。

这是我得到的:

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

控制器:

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

1 个答案:

答案 0 :(得分:0)

好的,我做到了(感谢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);
            }