2016-03-31 12 views
0

Ich habe eine NullReferenceException Fehler in meiner kleinen MVC 5-Anwendung. Ich habe die Beiträge gelesen, aber ich sehe immer noch nicht den Fehler, den ich gemacht habe. Create wirft eine NullReferenceException Ausnahme und wurde automatisch gerüstet.MVC NullReferenceException Fehler mit Standard-Gerüst

Ich habe etwas Code, der sich mit den anderen Eigenschaften befassen. Es wiederholt sich einfach.

@model WebApplication9.Models.Outreach 

@{ 
    ViewBag.Title = "Create"; 
} 

<h2>Create</h2> 

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

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

       <div class="form-group"> 
      <div class="col-md-offset-2 col-md-10"> 
       <input type="submit" value="Create" class="btn btn-default" /> 
      </div> 
     </div> 
    </div> 
} 

<div> 
    @Html.ActionLink("Back to List", "Index") 
</div> 

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

Der Controller:

// GET: Outreaches/Create 
     public ActionResult Create() 
     { 
      return View(); 
     } 

     // POST: Outreaches/Create 
     [HttpPost] 
     [ValidateAntiForgeryToken] 
     public ActionResult Create([Bind(Include = "TargetContact,NextOutreachStep,GoalOfOutreach,Currency,TalkingPoints,DueDate,PriorOutreach,Done,FollowUp")] Outreach outreach) 
     { 
      var test = string.Empty; 
      if (ModelState.IsValid) 
      { 
       db.Outreach.Add(outreach); 
       db.SaveChanges(); 
       return RedirectToAction("Index"); 
      } 

      return View(outreach); 
     } 

The Complete Exception Diese Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt in Englisch übersetzt bedeutet Object reference not set to an instance of an object

[NullReferenceException: Der Objektverweis wurde nicht auf eine Objektinstanz festgelegt.] 
    System.Web.Mvc.<ConvertResults>d__2.MoveNext() +56 
    System.Web.Mvc.<Validate>d__1.MoveNext() +562 
    System.Web.Mvc.DefaultModelBinder.OnModelUpdated(ControllerContext controllerContext, ModelBindingContext bindingContext) +120 
    System.Web.Mvc.DefaultModelBinder.BindComplexElementalModel(ControllerContext controllerContext, ModelBindingContext bindingContext, Object model) +68 
    System.Web.Mvc.DefaultModelBinder.BindComplexModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +1197 
    System.Web.Mvc.DefaultModelBinder.BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) +330 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValue(ControllerContext controllerContext, ParameterDescriptor parameterDescriptor) +338 
    System.Web.Mvc.ControllerActionInvoker.GetParameterValues(ControllerContext controllerContext, ActionDescriptor actionDescriptor) +105 
    System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__19(AsyncCallback asyncCallback, Object asyncState) +743 
    System.Web.Mvc.Async.WrappedAsyncResult`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +14 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.BeginInvokeAction(ControllerContext controllerContext, String actionName, AsyncCallback callback, Object state) +343 
    System.Web.Mvc.Controller.<BeginExecuteCore>b__1c(AsyncCallback asyncCallback, Object asyncState, ExecuteCoreState innerState) +25 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 
    System.Web.Mvc.Controller.BeginExecuteCore(AsyncCallback callback, Object state) +465 
    System.Web.Mvc.Controller.<BeginExecute>b__14(AsyncCallback asyncCallback, Object callbackState, Controller controller) +18 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +20 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 
    System.Web.Mvc.Controller.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +374 
    System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.BeginExecute(RequestContext requestContext, AsyncCallback callback, Object state) +16 
    System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__4(AsyncCallback asyncCallback, Object asyncState, ProcessRequestState innerState) +52 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallBeginDelegate(AsyncCallback callback, Object callbackState) +30 
    System.Web.Mvc.Async.WrappedAsyncResultBase`1.Begin(AsyncCallback callback, Object state, Int32 timeout) +128 
    System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +384 
    System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48 
    System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155 
+0

Gibt es zusätzliche Ausnahmeinformationen? An welchem ​​Punkt tritt die Ausnahme auf? Die erste Formularansicht oder nach dem Absenden? –

+0

Ich kann die Empy-Form sehen und wenn ich den create-Button drücke, bekomme ich die Exception. Ich habe versucht zu debuggen aber irgendwie denke ich, dass es nicht einmal die zweite Aktion erreicht. – user2675973

+0

Die vollständige Ausnahme hinzugefügt, wenn dies hilft – user2675973

Antwort

1

Genau das gleiche Problem hatte, dachte ich meine Lösung teilen würde. In meinem Fall implementierte mein Objekt IValidatableObject und in meiner Validierungsmethode gab ich null zurück. Das änderte mein Problem. Bis dahin wurde der Haltepunkt auf dem "POST" Aktionsergebnis nie getroffen, genau wie Sie beschrieben haben. Sogar die ersten paar Zeilen meiner Ausnahme sind identisch mit deiner.

Wenn Sie Ihre Situation betrachten, sieht es so aus, als hätten Sie einen String-Schlüssel. Gibt es irgendeine Chance, dass dein Modell das nicht mag oder hast du irgendeine Bestätigung wie ich?

Verwandte Themen