[ValidateInput(false)]
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(HttpPostedFileBase haberform)
{
NewsModel image = new NewsModel();
image.Tarih = DateTime.Now;
image.Kategori=Convert.ToInt32(Request.Form["Kategori"]);
image.Baslik =Request.Form["Baslik"];
image.Detay = Request.Form["Detay"];
image.Foto = Path.GetExtension(haberform.FileName);
db.NewsModels.Add(image);
db.SaveChanges();
string filePath = Path.Combine(HttpContext.Server.MapPath("../Content/images"), Path.GetFileName(image.id.ToString() + Path.GetExtension(haberform.FileName)));
haberform.SaveAs(filePath);
return RedirectToAction("Index");
}
@using (Html.BeginForm("Create", "News", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>NewsModel</h4>
<hr />
@Html.ValidationSummary(true)
@*<div class="form-group">
@Html.LabelFor(model => model.Tarih, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Tarih)
@Html.ValidationMessageFor(model => model.Tarih)
</div>
</div>*@
<div class="form-group">
@Html.LabelFor(model => model.Kategori, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Kategori)
@Html.ValidationMessageFor(model => model.Kategori)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Baslik, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Baslik)
@Html.ValidationMessageFor(model => model.Baslik)
</div>
</div>
@*<div class="form-group">
@Html.LabelFor(model => model.Foto, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.(model => model.Foto)
@Html.ValidationMessageFor(model => model.Foto)
</div>
</div>*@
<div class="form-group">
<label for="file">Upload Image:</label>
<div class="col-md-10">
<input name="haberform" type="file" />
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Detay, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Detay)
@Html.ValidationMessageFor(model => model.Detay)
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}
答案 0 :(得分:0)
用
解决了@"~/Content/images"