2015-07-09 110 views
0

尝试在窗体上保存编辑信息时出现以下错误。未将对象引用设置为对象的实例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") 
} 
+1

做一个调试版本,得到确切的错误细节(完整的例外细节,包括行号) – Amit

+0

这是一个常见的例外。在代码中......或者在其他相关的代码中可能几乎是_anything_。无法告诉没有更多的信息。 –

回答

1

很可能是因为MembershipTypeId您的型号是空的。我没有看到您的视图中的任何位置,在该位置绑定该值,如果不是,则不会将其作为提交的表单数据的一部分,因此不会在传递到控制器方法中的模型中填充。

最常见的修复方法是在窗体中添加一个Html.HiddenFor(m => m.MembershipTypeId)

P.S.不要告诉我们错误来自哪个行号是非常没有帮助的。

+0

非常感谢那正是它是什么,遗憾的是忘记说错误与哪里有关,从而让生活变得艰难。 –

相关问题