我的觀點mvc4實體framewok,設置空時我發佈編輯形式
@using(Html.BeginForm( 「編輯」, 「全球化志願服務青年」,FormMethod.Post,新的{ENCTYPE =表單數據」}) ) { @ Html.ValidationSummary(真)
<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>
}
我的控制器
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();
}
}
我嘗試更新我的數據庫,但是當我不選擇具有文件上傳一個文件,它設置爲null分貝。我CONTRO在後期編輯中填寫它,因爲它沒有設置空值,但它仍然設置。我怎樣才能修復它,或者如何加載我的編輯窗體時,我的數據庫中的路徑設置爲fileupload。
感謝您的幫助。
我試過hiddenfor,但是在這裏,即使我用fileUpload選擇了一個新文件,它也沒有更新。我想只是如果用戶選擇一個文件,它會更新,否則不會有任何東西。或者在頁面加載時,我想填充文件上傳組件。我怎樣才能做到這一點? – mrTurkay
@MuratTürkay我的不好,我只是注意到你的控制器方法不太對。看到我更新的答案。 – dom