如果没有在编辑上上传图像,则图像编辑将被覆盖



我开发了一个asp.net mvc应用程序。一旦一个记录被编辑,如果没有我们想要上传的编辑图像,现有的图像将被覆盖。有办法避免这种情况吗?

[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 validationn", 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>

我需要仔细检查你的视图代码,但是看起来上传的文件输入可能被称为ReceiptImage -如果是这样,当MVC做它的模型更新魔术时,将在提交上重置。AFAIK文件上传不需要是图像的字段名,尝试将文件输入控件的Id设置为不同的内容,看看它是否仍然覆盖值

最新更新