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 がモデル更新マジックを実行するときに送信時にリセットされます。私の知る限り、ファイルのアップロードは画像のフィールド名である必要はありません。ファイル入力コントロールの Id を別のものに設定してみて、それでも値が上書きされるかどうかを確認してください。

于 2013-06-03T13:44:32.507 に答える