My view
@using (Html.BeginForm("Edit", "Referance", FormMethod.Post, new { enctype = form-data" })) { @Html.ValidationSummary(true)
<fieldset>
<legend>Referance</legend>
@Html.HiddenFor(model => model.referanceId)
<div class="editor-label">
Dil
</div>
<div class="editor-field">
@Html.DropDownList("languageId", String.Empty)
@Html.ValidationMessageFor(model => model.languageId)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.name)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.name)
@Html.ValidationMessageFor(model => model.name)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.description)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.description)
@Html.ValidationMessageFor(model => model.description)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.path)
</div>
<div class="editor-field">
<input type="file" id="fuPhoto" name="fuPhoto" value="123" />
@Html.ValidationMessageFor(model => model.path)
</div>
<p>
<input type="submit" value="Save" />
</p>
</fieldset>
}
My Controller
public ActionResult Edit(int id = 0)
{
Referance oReferance = db.Referance.Find(id);
ViewBag.languageId = new SelectList(db.Language, "languageId", "name");
if (oReferance == null)
{
return HttpNotFound();
}
return View(oReferance);
}
[HttpPost]
public ActionResult Edit(HttpPostedFileBase fuPhoto, Referance oReferance)
{
try
{
if (ModelState.IsValid)
{
if (oReferance.path != null && fuPhoto == null)
oReferance.path = myHelper.saveFile(fuPhoto, "Uploads");
db.Entry(oReferance).State = EntityState.Modified;
db.SaveChanges();
}
return RedirectToAction("Index");
}
catch
{
return View();
}
}
I try update to my db but when I dont select a file with fileUpload, it sets null to db. I controlled it in post edit for it does not set null value, but it still sets. How can I fix it or how can set the path in my db to fileupload when load my edit form.
thanks for helps.