首页 > 解决方案 > 从 MVC 编辑页面/视图上传文件

问题描述

我有一个带有标准编辑视图的非常基本的 mvc 项目,我正在尝试添加直接从该视图上传文件的选项。

我已经从 Create 视图成功实现了上传选项,但由于某种原因它在 Edit 视图中不起作用。

编辑视图(文件上传和保存)

<div class="form-group">
    @Html.Label("Attach File", new { @class = "control-label col-md-2" })
    <div class="col-md-10">
        <input type="file" id="file" name="upload" />
    </div>
</div>

<div>
    <input type="submit" value="Save" class="btn btn-default" />
</div>

编辑控制器

    // POST: Travel/Edit/5
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see https://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Edit([Bind(Include = "TravelID,RequestedBy,DateRequested,TravelDescription,DestinationCity,DestinationState,StartDate,EndDate,EstRegistrationPerPerson,EstAirfarePerPerson,EstGroundPerPerson,EstLodgingPerPerson,EstMealsPerPerson,DepartureCity,DepartureState,DepartureDate,DepartureTimeRange,ReturnFromCity,ReturnFromState,ReturnFromDate,ReturnFromTimeRange,FlightNote,Hotel,HotelPhone,HotelNote,NeedHotelArrangements,NeedFlightArrangements,NeedRentalCarArrangements,IsTripInTravelBudget,Justification,OtherNote,SupAuth,SupAuthDate,CEOAuth,CEOAuthDate,ActRegistrationPerPerson,ActAirfarePerPerson,ActGroundPerPerson,ActLodgingPerPerson,ActMealsPerPerson")] Travel travel, HttpPostedFileBase upload)
    {
        if (ModelState.IsValid)
        {
            if (upload != null && upload.ContentLength > 0)
            {
                var file = new Files
                {
                    TravelID = travel.TravelID,
                    FileName = System.IO.Path.GetFileName(upload.FileName),
                    FileType = upload.ContentType
                };
                using (var reader = new System.IO.BinaryReader(upload.InputStream))
                {
                    file.Content = reader.ReadBytes(upload.ContentLength);
                }
                travel.Files = new List<Files> { file };
            }
            db.Entry(travel).State = EntityState.Modified;
            db.SaveChanges();
            return RedirectToAction("Details", "Travel", new { id = travel.TravelID });
        }
        return View(travel);
    }

当我提交编辑时,所有其他字段都会按预期更新,但没有上传文件。

文件型号:

namespace TrainingAndTravel.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Files
    {
        public int FileID { get; set; }
        public int TravelID { get; set; }
        public string FileName { get; set; }
        public string FileType { get; set; }
        public byte[] Content { get; set; }

        public virtual Travel Travel { get; set; }
    }
}

旅行模式:

namespace TrainingAndTravel.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;

    public partial class Travel
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
        public Travel()
        {
            this.Files = new HashSet<Files>();
            this.Travelers = new HashSet<Travelers>();
        }

        [Display(Name = "Travel ID")]
        public int TravelID { get; set; }

        [Display(Name = "Requested By")]
        public string RequestedBy { get; set; }

        [Display(Name = "Date Requested")]
        [DisplayFormat(DataFormatString = "{0:g}", ApplyFormatInEditMode = true)]
        public System.DateTime DateRequested { get; set; }

        [Display(Name = "Travel Description")]
        public string TravelDescription { get; set; }

        [Display(Name = "Destination City")]
        public string DestinationCity { get; set; }

        [Display(Name = "Destination State")]
        public string DestinationState { get; set; }

        [Display(Name = "Start Date")]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
        public Nullable<System.DateTime> StartDate { get; set; }

        [Display(Name = "End Date")]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
        public Nullable<System.DateTime> EndDate { get; set; }

        [Display(Name = "Estimated Registration Per Person")]
        public Nullable<decimal> EstRegistrationPerPerson { get; set; }

        [Display(Name = "Estimated Airfare Per Person")]
        public Nullable<decimal> EstAirfarePerPerson { get; set; }

        [Display(Name = "Estimated Ground Per Person")]
        public Nullable<decimal> EstGroundPerPerson { get; set; }

        [Display(Name = "Estimated Lodging Per Person")]
        public Nullable<decimal> EstLodgingPerPerson { get; set; }

        [Display(Name = "Estimated Meals Per Person")]
        public Nullable<decimal> EstMealsPerPerson { get; set; }

        [Display(Name = "Departure City")]
        public string DepartureCity { get; set; }

        [Display(Name = "Departure State")]
        public string DepartureState { get; set; }

        [Display(Name = "Departure Date")]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
        public Nullable<System.DateTime> DepartureDate { get; set; }

        [Display(Name = "Departure Time Range")]
        public string DepartureTimeRange { get; set; }

        [Display(Name = "Return From City")]
        public string ReturnFromCity { get; set; }

        [Display(Name = "Return From State")]
        public string ReturnFromState { get; set; }

        [Display(Name = "Return From Date")]
        [DisplayFormat(DataFormatString = "{0:MM/dd/yyyy}", ApplyFormatInEditMode = true)]
        public Nullable<System.DateTime> ReturnFromDate { get; set; }

        [Display(Name = "Return From Time Range")]
        public string ReturnFromTimeRange { get; set; }

        [Display(Name = "Flight Note")]
        public string FlightNote { get; set; }

        public string Hotel { get; set; }

        [Display(Name = "Hotel Phone")]
        public string HotelPhone { get; set; }

        [Display(Name = "Hotel Note")]
        public string HotelNote { get; set; }

        [Display(Name = "Need Hotel Arrangements")]
        public bool NeedHotelArrangements { get; set; }

        [Display(Name = "Need Flight Arrangements")]
        public bool NeedFlightArrangements { get; set; }

        [Display(Name = "Need Rental Car Arrangements")]
        public bool NeedRentalCarArrangements { get; set; }

        [Display(Name = "Is Trip In Travel Budget")]
        public bool IsTripInTravelBudget { get; set; }

        public string Justification { get; set; }

        [Display(Name = "Other Note")]
        public string OtherNote { get; set; }

        [Display(Name = "Supervisor Authorization")]
        public string SupAuth { get; set; }

        [Display(Name = "Supervisor Authorization Date & Time")]
        [DisplayFormat(DataFormatString = "{0:g}", ApplyFormatInEditMode = true)]
        public Nullable<System.DateTime> SupAuthDate { get; set; }

        [Display(Name = "CEO Authorization")]
        public string CEOAuth { get; set; }

        [Display(Name = "CEO Authorization Date & Time")]
        [DisplayFormat(DataFormatString = "{0:g}", ApplyFormatInEditMode = true)]
        public Nullable<System.DateTime> CEOAuthDate { get; set; }

        [Display(Name = "Actual Registration Per Person")]
        public Nullable<decimal> ActRegistrationPerPerson { get; set; }

        [Display(Name = "Actual Airfare Per Person")]
        public Nullable<decimal> ActAirfarePerPerson { get; set; }

        [Display(Name = "Actual Ground Per Person")]
        public Nullable<decimal> ActGroundPerPerson { get; set; }

        [Display(Name = "Actual Lodging Per Person")]
        public Nullable<decimal> ActLodgingPerPerson { get; set; }

        [Display(Name = "Actual Meals Per Person")]
        public Nullable<decimal> ActMealsPerPerson { get; set; }

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Files> Files { get; set; }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
        public virtual ICollection<Travelers> Travelers { get; set; }
    }
}

整个编辑视图:

@model TrainingAndTravel.Models.Travel

@{
    ViewBag.Title = "Edit";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

<h2>Edit Travel Request</h2>


@using (Html.BeginForm())
{
    @Html.AntiForgeryToken()

    <div class="form-horizontal">
        <hr />
        @Html.ValidationSummary(true, "", new { @class = "text-danger" })
        @Html.HiddenFor(model => model.TravelID)

        <div hidden class="form-group">
            @Html.LabelFor(model => model.RequestedBy, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.RequestedBy, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                @Html.ValidationMessageFor(model => model.RequestedBy, "", new { @class = "text-danger" })
            </div>
        </div>

        <div hidden class="form-group">
            @Html.LabelFor(model => model.DateRequested, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DateRequested, new { htmlAttributes = new { @class = "form-control", @readonly = "readonly" } })
                @Html.ValidationMessageFor(model => model.DateRequested, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.TravelDescription, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.TravelDescription, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.TravelDescription, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DestinationCity, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DestinationCity, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DestinationCity, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DestinationState, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DestinationState, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DestinationState, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.StartDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.StartDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.StartDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EndDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EndDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EndDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EstRegistrationPerPerson, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EstRegistrationPerPerson, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EstRegistrationPerPerson, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EstAirfarePerPerson, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EstAirfarePerPerson, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EstAirfarePerPerson, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EstGroundPerPerson, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EstGroundPerPerson, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EstGroundPerPerson, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EstLodgingPerPerson, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EstLodgingPerPerson, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EstLodgingPerPerson, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.EstMealsPerPerson, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.EstMealsPerPerson, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.EstMealsPerPerson, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DepartureCity, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DepartureCity, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DepartureCity, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DepartureState, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DepartureState, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DepartureState, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DepartureDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.DepartureDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.DepartureDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.DepartureTimeRange, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.DepartureTimeRange, TrainingAndTravel.Utils.TimeRangeList.GetTimeRangeList(), new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.DepartureTimeRange, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ReturnFromCity, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ReturnFromCity, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ReturnFromCity, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ReturnFromState, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ReturnFromState, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ReturnFromState, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ReturnFromDate, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ReturnFromDate, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ReturnFromDate, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.ReturnFromTimeRange, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.DropDownListFor(model => model.ReturnFromTimeRange, TrainingAndTravel.Utils.TimeRangeList.GetTimeRangeList(), new { @class = "form-control" })
                @Html.ValidationMessageFor(model => model.ReturnFromTimeRange, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.FlightNote, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.FlightNote, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.FlightNote, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Hotel, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Hotel, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Hotel, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.HotelPhone, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HotelPhone, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.HotelPhone, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.HotelNote, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.HotelNote, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.HotelNote, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.Justification, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.Justification, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.Justification, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.OtherNote, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.OtherNote, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.OtherNote, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NeedHotelArrangements, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.CheckBoxFor(model => model.NeedHotelArrangements, new { @class = "checkbox" })
                @Html.ValidationMessageFor(model => model.NeedHotelArrangements, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NeedFlightArrangements, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.CheckBoxFor(model => model.NeedFlightArrangements, new { @class = "checkbox" })
                @Html.ValidationMessageFor(model => model.NeedFlightArrangements, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.NeedRentalCarArrangements, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.CheckBoxFor(model => model.NeedRentalCarArrangements, new { @class = "checkbox" })
                @Html.ValidationMessageFor(model => model.NeedRentalCarArrangements, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.LabelFor(model => model.IsTripInTravelBudget, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.CheckBoxFor(model => model.IsTripInTravelBudget, new { @class = "checkbox" })
                @Html.ValidationMessageFor(model => model.IsTripInTravelBudget, "", new { @class = "text-danger" })
            </div>
        </div>

        <div hidden class="form-group">
            @Html.LabelFor(model => model.ActRegistrationPerPerson, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ActRegistrationPerPerson, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ActRegistrationPerPerson, "", new { @class = "text-danger" })
            </div>
        </div>

        <div hidden class="form-group">
            @Html.LabelFor(model => model.ActAirfarePerPerson, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ActAirfarePerPerson, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ActAirfarePerPerson, "", new { @class = "text-danger" })
            </div>
        </div>

        <div hidden class="form-group">
            @Html.LabelFor(model => model.ActGroundPerPerson, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ActGroundPerPerson, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ActGroundPerPerson, "", new { @class = "text-danger" })
            </div>
        </div>

        <div hidden class="form-group">
            @Html.LabelFor(model => model.ActLodgingPerPerson, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ActLodgingPerPerson, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ActLodgingPerPerson, "", new { @class = "text-danger" })
            </div>
        </div>

        <div hidden class="form-group">
            @Html.LabelFor(model => model.ActMealsPerPerson, htmlAttributes: new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                @Html.EditorFor(model => model.ActMealsPerPerson, new { htmlAttributes = new { @class = "form-control" } })
                @Html.ValidationMessageFor(model => model.ActMealsPerPerson, "", new { @class = "text-danger" })
            </div>
        </div>

        <div class="form-group">
            @Html.Label("Attach File", new { @class = "control-label col-md-2" })
            <div class="col-md-10">
                <input type="file" id="file" name="upload" />
            </div>
        </div>

        <div>
            <input type="submit" value="Save" class="btn btn-default" />
            @Html.ActionLink("Cancel", "Details", "Travel", new { id = Model.TravelID }, new { @class = "btn btn-default" })
        </div>

    </div>

}

@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

标签: c#asp.net-mvcentity-framework

解决方案


我不得不重新排列我的控制器,将上传部分放在 EntityState.Modified 和 SaveChanges 下方,还需要在上传下方有一个 db.SaveChanges():

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Edit([Bind(Include = "TravelID,RequestedBy,DateRequested,TravelDescription,DestinationCity,DestinationState,StartDate,EndDate,EstRegistrationPerPerson,EstAirfarePerPerson,EstGroundPerPerson,EstLodgingPerPerson,EstMealsPerPerson,DepartureCity,DepartureState,DepartureDate,DepartureTimeRange,ReturnFromCity,ReturnFromState,ReturnFromDate,ReturnFromTimeRange,FlightNote,Hotel,HotelPhone,HotelNote,NeedHotelArrangements,NeedFlightArrangements,NeedRentalCarArrangements,IsTripInTravelBudget,Justification,OtherNote,SupAuth,SupAuthDate,CEOAuth,CEOAuthDate,ActRegistrationPerPerson,ActAirfarePerPerson,ActGroundPerPerson,ActLodgingPerPerson,ActMealsPerPerson")] Travel travel, HttpPostedFileBase upload)
{
    if (ModelState.IsValid)
    {
        db.Entry(travel).State = EntityState.Modified;
        db.SaveChanges();
        if (upload != null && upload.ContentLength > 0)
        {
            var file = new Files
            {
                FileName = System.IO.Path.GetFileName(upload.FileName),
                FileType = upload.ContentType
            };
            using (var reader = new System.IO.BinaryReader(upload.InputStream))
            {
                file.Content = reader.ReadBytes(upload.ContentLength);
            }
            travel.Files = new List<Files> { file };
        }
        db.SaveChanges();
        return RedirectToAction("Details", "Travel", new { id = travel.TravelID });
    }
    return View(travel);
}

推荐阅读