asp.net mvc3如果在Edit上没有上传图像,图像编辑将被覆盖

时间:2013-06-03 13:21:35

标签: asp.net asp.net-mvc-3 file

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

此致

1 个答案:

答案 0 :(得分:0)

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