3

我开发了一个 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>

问候

4

1 回答 1

0

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

于 2013-06-03T13:44:32.507 回答