我的观点
@using(Html.BeginForm(“Edit”,“Referance”,FormMethod.Post,new {enctype = form-data“})) { @ 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();
}
}
我尝试更新到我的数据库但是当我不选择带有fileUpload的文件时,它将null设置为db。我在后期编辑中控制它,因为它没有设置空值,但它仍然设置。如何修复它或如何在加载我的编辑表单时将我的数据库中的路径设置为fileupload。
感谢帮助。
答案 0 :(得分:1)
只需为文件属性添加隐藏字段。
<div class="editor-field">
<input type="file" id="fuPhoto" name="fuPhoto" value="123" />
@Html.HiddenFor(model => model.path)
@Html.ValidationMessageFor(model => model.path)
</div>
这样,该值将与表单的其余部分一起发布,然后使用控制器操作中的已发布文件进行更新。
更新了控制器:
public ActionResult Edit(HttpPostedFileBase fuPhoto, Referance oReferance)
{
try
{
if (ModelState.IsValid)
{
// at this point oReferance.path contains the value that was posted with the hidden field
// if a file was selected, upload and update the model's path property
if (fuPhoto != null)
oReferance.path = myHelper.saveFile(fuPhoto, "Uploads");
// save model
db.Entry(oReferance).State = EntityState.Modified;
db.SaveChanges();
return RedirectToAction("Index");
}
catch
{
return View();
}
}
}
正如您所看到的,我删除了oReferance.path != null
语句的if
,因为您只需要测试是否上传了新文件,旧值是无关紧要的,如果有,则会保留没有选择文件。
答案 1 :(得分:1)
直接从模型绑定对象执行此操作时,您告诉EF覆盖oReferance对象中的所有属性。
if ( fuPhoto == null)
oReferance.path = myHelper.saveFile(fuPhoto, "Uploads");
db.Entry(oReferance).State = EntityState.Modified;
db.SaveChanges();
由于您没有将oRefreance.path中的directlry绑定到视图中,因此post方法中的路径不再可用。
您可以在视图中添加隐藏 @if(!string.IsNullorEmpty(Model.path)) { Model.path }
<input type="file" id="fuPhoto" name="fuPhoto" value="123" />
@Html.HiddenFor(model=> model.path)
@Html.ValidationMessageFor(model => model.path)
这将确保路径绑定回OReference对象。