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