c# - Why can only one action not see HTMLHelper? -


i'm trying make simple asp.net-mvc application. i've made model class , added database. used built-in way generate controller views using entity framework. reason, when try view create page (and create page, not edit page!) null reference exception (object reference not set instance of object).

so here's create action:

    public actionresult create()     {         return view();     } 

here's edit action:

    public actionresult edit(int? id)     {         if (id == null)         {             return new httpstatuscoderesult(httpstatuscode.badrequest);         }         suppliercorrection suppliercorrection = db.suppliercorrectionset.find(id);         if (suppliercorrection == null)         {             return httpnotfound();         }         return view(suppliercorrection);     } 

here's create page:

@model fair.data.models.suppliercorrection  @{     viewbag.title = "create"; }  <h2>create</h2>  @using (html.beginform()) {     @html.antiforgerytoken()      <div class="form-horizontal">         <h4>suppliercorrection</h4>         <hr />         @html.validationsummary(true, "", new { @class = "text-danger" })          @html.partial("_createedit", model)          <div class="form-group">             <div class="col-md-offset-2 col-md-10">                 <input type="submit" value="save" class="btn btn-default" />             </div>         </div>     </div> }  <div>     @html.actionlink("back list", "index") </div>  @section scripts{     <script type="text/javascript" src="~/scripts/suppliercorrectionscreateedit.js"></script> } 

here's edit page (the astute reader notice it's similar):

@model fair.data.models.suppliercorrection  @{     viewbag.title = "edit"; }  <h2>edit</h2>  @using (html.beginform()) {     @html.antiforgerytoken()      <div class="form-horizontal">         <h4>suppliercorrection</h4>         <hr />         @html.validationsummary(true, "", new { @class = "text-danger" })         @html.hiddenfor(model => model.id)          @html.partial("_createedit", model)          <div class="form-group">             <div class="col-md-offset-2 col-md-10">                 <input type="submit" value="save" class="btn btn-default" />             </div>         </div>     </div> }  <div>     @html.actionlink("back list", "index") </div>  @section scripts{     <script type="text/javascript" src="~/scripts/suppliercorrectionscreateedit.js"></script> } 

and here's _createedit:

@model fair.data.models.suppliercorrection  <div class="form-group">     @html.labelfor(model => model.fairnumber, htmlattributes: new { @class = "control-label col-md-2" })     <div class="col-md-10">         @html.editorfor(model => model.fairnumber, new { htmlattributes = new { @class = "form-control" } })         @html.validationmessagefor(model => model.fairnumber, "", new { @class = "text-danger" })     </div> </div>  <div class="form-group">     @html.labelfor(model => model.supplier, htmlattributes: new { @class = "control-label col-md-2" })     <div class="col-md-10">         @html.editorfor(model => model.supplier, new { htmlattributes = new { @class = "form-control" } })         @html.validationmessagefor(model => model.supplier, "", new { @class = "text-danger" })     </div> </div>  <div class="form-group">     @html.labelfor(model => model.closed, htmlattributes: new { @class = "control-label col-md-2" })     <div class="col-md-10">         <div class="checkbox">             @html.editorfor(model => model.closed)             @html.validationmessagefor(model => model.closed, "", new { @class = "text-danger" })         </div>     </div> </div>  <div class="form-group">     <label class="control-label col-md-2">corrections link</label>     <div class="col-md-10">         <input type="file" id="correctionslinkfilepicker" onchange="correctionslinkchosen();" />         <input type="hidden" name="correctionslink" id="correctionslink" value="@model.correctionslink" />         @if (!string.isnullorwhitespace(model?.correctionslink))         {             @:currently: <a href="@model.correctionslink">@model.correctionslink</a>         }         @html.validationmessagefor(model => model.correctionslink, "", new { @class = "text-danger" })     </div> </div> 

when navigate create page (again, not edit page) errors on line 24, html.validationmesagefor(model => model.closed,... if remove that, errors on previous line. , on until i'm down using(html.beginform()). guess stopgap can rewrite page without using html object @ all, in case happens again (or on longer page) i'd know why in world went null.

full stack trace:

   @ asp._page_views_suppliercorrections__createedit_cshtml.execute() in c:\users\ttm920632\desktop\repos\fair\fair\views\suppliercorrections\_createedit.cshtml:line 24    @ system.web.webpages.webpagebase.executepagehierarchy()    @ system.web.mvc.webviewpage.executepagehierarchy()    @ system.web.webpages.webpagebase.executepagehierarchy(webpagecontext pagecontext, textwriter writer, webpagerenderingbase startpage)    @ system.web.mvc.razorview.renderview(viewcontext viewcontext, textwriter writer, object instance)    @ system.web.mvc.buildmanagercompiledview.render(viewcontext viewcontext, textwriter writer)    @ system.web.mvc.htmlhelper.renderpartialinternal(string partialviewname, viewdatadictionary viewdata, object model, textwriter writer, viewenginecollection viewenginecollection)    @ system.web.mvc.html.partialextensions.partial(htmlhelper htmlhelper, string partialviewname, object model, viewdatadictionary viewdata)    @ system.web.mvc.html.partialextensions.partial(htmlhelper htmlhelper, string partialviewname, object model)    @ asp._page_views_suppliercorrections_create_cshtml.execute() in c:\users\ttm920632\desktop\repos\fair\fair\views\suppliercorrections\create.cshtml:line 18    @ system.web.webpages.webpagebase.executepagehierarchy()    @ system.web.mvc.webviewpage.executepagehierarchy()    @ system.web.webpages.startpage.runpage()    @ system.web.webpages.startpage.executepagehierarchy()    @ system.web.webpages.webpagebase.executepagehierarchy(webpagecontext pagecontext, textwriter writer, webpagerenderingbase startpage)    @ system.web.mvc.razorview.renderview(viewcontext viewcontext, textwriter writer, object instance)    @ system.web.mvc.buildmanagercompiledview.render(viewcontext viewcontext, textwriter writer)    @ system.web.mvc.viewresultbase.executeresult(controllercontext context)    @ system.web.mvc.controlleractioninvoker.invokeactionresult(controllercontext controllercontext, actionresult actionresult)    @ system.web.mvc.controlleractioninvoker.invokeactionresultfilterrecursive(ilist`1 filters, int32 filterindex, resultexecutingcontext precontext, controllercontext controllercontext, actionresult actionresult)    @ system.web.mvc.controlleractioninvoker.invokeactionresultfilterrecursive(ilist`1 filters, int32 filterindex, resultexecutingcontext precontext, controllercontext controllercontext, actionresult actionresult) 


Comments

Popular posts from this blog

What is happening when Matlab is starting a "parallel pool"? -

angular - DownloadURL return null in below code -

php - Cannot override Laravel Spark authentication with own implementation -