我开发了一个 asp.net MVC3 应用程序。编辑记录后,如果编辑中没有我们要上传的图像,则现有图像将被覆盖。关于如何避免这种情况的任何想法?
[HttpPost]
public ActionResult Edit(PaymentSchedule paymentschedule, HttpPostedFileBase file)
{
if (ModelState.IsValid)
{
if (file != null)
{
paymentschedule.ReceiptImage = new byte[file.ContentLength];
paymentschedule.ReceiptImageType = file.ContentType;
BinaryReader reader = new BinaryReader(file.InputStream);
paymentschedule.ReceiptImage = reader.ReadBytes(file.ContentLength);
}
db.Entry(paymentschedule).State = EntityState.Modified;
try
{ db.SaveChanges();
}
catch (DbEntityValidationException ex)
{
var sb = new System.Text.StringBuilder();
foreach (var failure in ex.EntityValidationErrors)
{
sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
foreach (var error in failure.ValidationErrors)
{
sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
sb.AppendLine();
}
}
throw new DbEntityValidationException(
"Entity Validation Failed - errors follow:\n" +
sb.ToString(), ex
); // Add the original exception as the innerException
}
return RedirectToAction("SearchCust");
}
return View(paymentschedule);
}
要编辑的视图是正常提交。任何想法?
这是编辑视图的代码:
@model fbpm.Models.PaymentSchedule
@{
ViewBag.Title = "Edit Payment Schedule";
}
<h2>Edit Payment Schedule</h2>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@using (Html.BeginForm("Edit", "PaymentSchedule", FormMethod.Post, new
{
enctype = "multipart/form-data"
, id = "parentForm"
})) {
@Html.ValidationSummary(true)
<fieldset>
<legend>Payment Schedule</legend>
<div style="float:left; width:400px">
<div class="editor-label">
@Html.LabelFor(model => model.ScheduleID)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.ScheduleID)
@Html.HiddenFor(model => model.ScheduleID)
@Html.ValidationMessageFor(model => model.ScheduleID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.UserID)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.UserID)
@Html.HiddenFor(model => model.UserID)
@Html.ValidationMessageFor(model => model.UserID)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ScheduleText)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ScheduleText)
@Html.ValidationMessageFor(model => model.ScheduleText)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ScheduleDate)
</div>
<div class="editor-field">
@Html.TextBoxFor(model => model.ScheduleDate)
@Html.ValidationMessageFor(model => model.ScheduleDate)
</div>
<p>
<input class="btn btn-primary btn-large" type="submit" value="Save Payment Schedule" />
</p>
</div>
<div style="float:left; width:400px">
<div class="editor-label">
@Html.LabelFor(model => model.SchedulePercentage)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.SchedulePercentage)
@Html.ValidationMessageFor(model => model.SchedulePercentage)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.ScheduleAmount)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.ScheduleAmount)
@Html.ValidationMessageFor(model => model.ScheduleAmount)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.RemainingAmount)
</div>
<div class="editor-field">
@Html.DisplayFor(model => model.RemainingAmount)
@Html.HiddenFor(model => model.RemainingAmount)
@Html.ValidationMessageFor(model => model.RemainingAmount)
</div>
</div>
<div style="float:left; width:400px">
<div class="editor-field">
<img src='/fbpm/fbpm/PaymentSchedule/GetImage/@Html.DisplayFor(model => model.ScheduleID)' width="300" height="300" alt="" />
</div>
<div class="editor-field">
@Html.LabelFor(model => model.ReceiptImage)
@Html.HiddenFor(model => model.ReceiptImage)
</div>
@Html.HiddenFor(model => model.ReceiptImageType)
<input id="file" title="Upload a Receipt Image" type="file" name="file" />
</div>
</fieldset>
}
<div>
@Html.ActionLink("Back to List", "SearchCust")
</div>
问候