嘗試在窗體上保存編輯信息時出現以下錯誤。未將對象引用設置爲對象的實例asp.net mvc
類型「System.NullReferenceException」的第一次機會異常發生在GRCWebApp.dll
其他信息:對象沒有設置爲一個對象的一個實例。
錯誤與控制器中的此行有關:currentMembershipType.Type = model.Type;
POST方法
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult EditClubMembershipType(EditMembershipTypeViewModel model)
{
if (ModelState.IsValid)
{
//Go fetch the membership type from the database
var currentMembershipType = db.MembershipTypes.FirstOrDefault(p => p.MembershipTypeId == model.MembershipTypeId);
// Save new Membership Type
currentMembershipType.Type = model.Type;
currentMembershipType.Description = model.Description;
currentMembershipType.Cost = model.Cost;
currentMembershipType.ReducedCost = model.ReducedCost;
currentMembershipType.DayId = model.ReducedDay;
currentMembershipType.MonthId = model.ReducedMonth;
currentMembershipType.MinAge = model.MinAge;
currentMembershipType.MaxAge = model.MaxAge;
// Save Changes
db.SaveChanges();
return RedirectToAction("AddClubMembershipType", new { clubId = model.ClubId, editMode = true });
}
return View("Error");
}
的觀點是:
@model GRCWebApp.ViewModels.EditMembershipTypeViewModel
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.HiddenFor(model => model.ClubId)
<div class="col-md-12">
<h3>Edit Membership Type</h3>
</div>
<div class="form-horizontal">
<hr />
<div class="row">
<div class="col-md-10">
<div class="well bs-component">
<div class="form-group">
</div>
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
<div class="row col-offset-md-1 col-md-11">
<div class="col-md-3">
@Html.LabelFor(model => model.Type, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Type, new { htmlAttributes = new { @class = "form-control", placeholder = "e.g. Full" } })
@Html.ValidationMessageFor(model => model.Type, "", new { @class = "text-danger" })
</div>
<div class="col-md-4">
@Html.LabelFor(model => model.Description, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.Description, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Description, "", new { @class = "text-danger" })
</div>
<div class="col-md-2">
@Html.LabelFor(model => model.Cost, htmlAttributes: new { @class = "control-label" })
<div class="input-group">
<div class="input-group-addon"> £</div>@Html.EditorFor(model => model.Cost, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Cost, "", new { @class = "text-danger" })
</div>
</div>
</div>
</div>
<h4>Do you offer reduced membership cost part way through the year? Add details here:</h4>
<div class="form-group">
<div class="row col-offset-md-1 col-md-11">
<div class="col-md-2">
@Html.LabelFor(model => model.ReducedCost, htmlAttributes: new { @class = "control-label" })
<div class="input-group">
<div class="input-group-addon">£</div>
@Html.EditorFor(model => model.ReducedCost, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.ReducedCost, "", new { @class = "text-danger" })
</div>
</div>
<div class="col-md-2">
@Html.LabelFor(model => model.ReducedDay, htmlAttributes: new { @class = "control-label" })
@Html.DropDownListFor(model => model.ReducedDay, new SelectList(Model.Days, "DayId", "DayNum", 1), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ReducedDay, "", new { @class = "text-danger" })
</div>
<div class="col-md-2">
@Html.LabelFor(model => model.ReducedMonth, htmlAttributes: new { @class = "control-label" })
@Html.DropDownListFor(model => model.ReducedMonth, new SelectList(Model.Months, "MonthId", "MonthName"), new { @class = "form-control" })
@Html.ValidationMessageFor(model => model.ReducedMonth, "", new { @class = "text-danger" })
</div>
</div>
</div>
<h4>Are there age restrictions for this Membership Type? Add details here:</h4>
<div class="form-group">
<div class="row col-offset-md-1 col-md-11">
<div class="col-md-2">
@Html.LabelFor(model => model.MinAge, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.MinAge, new { htmlAttributes = new { @class = "form-control", @Value = "1" } })
@Html.ValidationMessageFor(model => model.MinAge, "", new { @class = "text-danger" })
</div>
<div class="col-md-2">
@Html.LabelFor(model => model.MaxAge, htmlAttributes: new { @class = "control-label" })
@Html.EditorFor(model => model.MaxAge, new { htmlAttributes = new { @class = "form-control", @Value = "150" } })
@Html.ValidationMessageFor(model => model.MaxAge, "", new { @class = "text-danger" })
</div>
</div>
</div>
<div class="form-group">
<div class=" col-md-10">
<input type="submit" name="Submit" value="Save" class="btn btn-success btn-lg" />
</div>
</div>
</div>
</div>
</div>
</div>
}
<div>
@Html.ActionLink("Back to Membership Types", "AddClubMembershipType", new { clubId = Model.ClubId, editMode = true })
</div>
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
做一個調試版本,得到確切的錯誤細節(完整的例外細節,包括行號) – Amit
這是一個常見的例外。在代碼中......或者在其他相關的代碼中可能幾乎是_anything_。無法告訴沒有更多的信息。 –