编辑页面-对象引用未设置为对象的实例

时间:2018-07-10 14:50:16

标签: .net asp.net-mvc

我遇到了编辑功能添加一个新条目而不是更新数据的问题,但是现在我更改了代码,它给了我如下错误:

object reference not set to an instance of an object

我已经尝试过返回视图模型,但是它仍然不能像更改存储库中的代码那样有效,但是仍然无法使它正常工作。我已经阅读了同一问题的一些答案,但是并不能解决我的问题。如果您仍然需要有关该程序的信息,请务必告诉我,谢谢!!

控制器:

   //EDIT
    [HttpGet]
    public ActionResult Edit(int id)
    {
        var repo = new ManagementRepository();
        var DVM = new DriverViewModel();

        Driver editDriver = db.Drivers.Find(id);
    //   repo.UpdateDriver(editDriver);
        return View(DVM);
    }

    [HttpPost]
    public ActionResult Edit(DriverViewModel DVM)
    {

        var repo = new ManagementRepository();
        var driver = new Driver();
        using (var db = new VehicleReservationEntities())
        { 
            var existingDriver = (from data in db.Drivers
                                  where data.DriverID == DVM.DriverID
                                  select data).FirstOrDefault();

            existingDriver.DriverID = DVM.DriverID;
            existingDriver.DriverLastName = DVM.DriverLastName;
            existingDriver.DriverFirstName = DVM.DriverFirstName;
            existingDriver.DriverLicense = DVM.DriverLicense;
            existingDriver.LicenseExpiry = DVM.LicenseExpiry;
            existingDriver.MobileNumber = DVM.MobileNumber;
            existingDriver.BusinessUnit = DVM.BusinessUnit;

            repo.UpdateDriver(existingDriver);
        }




        return View(DVM);

    }
}

查看模型:

    public class DriverViewModel
{
    public int DriverID { get; set; }
    public string DriverLastName { get; set; }
    public string DriverFirstName { get; set; }
    public string MobileNumber { get; set; }
    public string DriverLicense { get; set; }
    public Nullable<System.DateTime> LicenseExpiry { get; set; }
    public string BusinessUnit { get; set; }
    public bool IsActive { get; set; }
    public System.DateTime DateRegistered { get; set; }

    public string FullName
    {
        get { return DriverLastName + ", " + DriverFirstName; }
    }
}

查看:

    @model VehicleReservation.ViewModels.DriverViewModel

@{
    ViewBag.Title = "Edit";
}

@section Jscripts{

    <script type="text/javascript">
        $(function () {
            $("#LicenseExpiry").datepicker({
                changeMonth: true,
                changeYear: true,
                format: "yyyy-MM-dd"

            }
            ).datepicker("setDate", new DateTime());
        });
    </script>

    <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>

}

<h2>Edit Driver</h2>



<div class="row">
    <div class="col-md-12 ">

        <div class="panel" style="margin-top:3%; margin-bottom: 5%;">
            <div class="panel-heading">
                <div class="panel-title">
                </div>

                <div class="panel-body">

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

                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

                        @Html.HiddenFor(model => model.DriverID)
                        @Html.HiddenFor(model => model.DateRegistered)                        @Html.HiddenFor(model => model.DateRegistered)
                        @Html.HiddenFor(model => model.IsActive)


                        <div class="col-md-12">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label">
                                        <label>
                                            Last Name
                                        </label>
                                    </div>
                                    <div>
                                        @Html.EditorFor(model => model.DriverLastName, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.DriverLastName, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>

                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label">
                                        <label>
                                            First Name
                                        </label>
                                    </div>
                                    <div>
                                        @Html.EditorFor(model => model.DriverFirstName, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.DriverFirstName, "", new { @class = "text-danger" })

                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label">
                                        <label>
                                            Mobile Number
                                        </label>
                                    </div>
                                    <div>
                                        @Html.EditorFor(model => model.MobileNumber, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.MobileNumber, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label">
                                        <label>
                                            Driver's License
                                        </label>
                                    </div>
                                    <div>
                                        @Html.EditorFor(model => model.DriverLicense, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.DriverLicense, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>

                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label">
                                        <label>
                                            License Expiry
                                        </label>
                                    </div>
                                    <div>
                                        @Html.EditorFor(model => model.LicenseExpiry, new { htmlAttributes = new { @class = "form-control datepicker", @autocomplete = "off" } })
                                        @Html.ValidationMessageFor(model => model.LicenseExpiry, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label ">
                                        <label>
                                            Business Unit ID
                                        </label>
                                    </div>
                                    <div>
                                        @Html.EditorFor(model => 
    model.BusinessUnit, new { htmlAttributes = new { @class = "form-control", @type = "text", @style = "width:50%" } })
                                        @Html.ValidationMessageFor(model => model.BusinessUnit, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                        </div>




                        <div class="form-group">
                            <div class="col-sm-12">
                                <input type="submit" value="Save" class="btn btn-success" /> |
                                @Html.ActionLink("Back To List", "List", null, new { @class = "btn btn-primary btn-large" })
                            </div>
                        </div>
                    }

                </div>
            </div>

        </div>

    </div>




</div>

存储库:

  public void UpdateDriver(Driver editDriver)
{
              using (var db = new VehicleReservationEntities())
               {
                  // var existingDriver = (from data in db.Drivers
                  //                       where data.DriverID == editDriver.DriverID
                  //                       select data).FirstOrDefault();

                   db.Entry(editDriver).State = EntityState.Modified;
                   db.SaveChanges();
               }

    /*            using (var db = new VehicleReservationEntities())
               {

                   db.Entry(id).State = EntityState.Modified;
                   db.SaveChanges();
               }
   */


}   

1 个答案:

答案 0 :(得分:1)

Public Sub TestMe()

    Dim arrColors As Variant
    arrColors = Array(12, 2, 3, 4, 5)    'these are colors

    Dim i As Long
    For i = LBound(arrColors) To UBound(arrColors)
        Worksheets(i + 1).Tab.ThemeColor = arrColors(i)
    Next i

End Sub

如果数据库中没有具有该ID的驱动程序,则此行将返回“ null”。以后,这样做会导致空引用异常:

        var existingDriver = (from data in db.Drivers
                              where data.DriverID == DVM.DriverID
                              select data).FirstOrDefault();