this question has answer here:
- what nullreferenceexception, , how fix it? 29 answers
i'm getting following error when trying save edited information on form.
a first chance exception of type 'system.nullreferenceexception' occurred in grcwebapp.dll
additional information: object reference not set instance of object.
the error related line in controller : currentmembershiptype.type = model.type;
the post method is
[httppost] [validateantiforgerytoken] public actionresult editclubmembershiptype(editmembershiptypeviewmodel model) { if (modelstate.isvalid) { //go fetch membership type 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"); } the view is:
@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 offer reduced membership cost part way through 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 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 membership types", "addclubmembershiptype", new { clubid = model.clubid, editmode = true }) </div> @section scripts { @scripts.render("~/bundles/jqueryval") }
it's because membershiptypeid on model null. don't see anywhere in view value being bound, , if it's not, not part of submitted form data , therefore not populated in model passed httppost controller method.
most common fix add html.hiddenfor(m => m.membershiptypeid) in form.
p.s. it's unhelpful not tell line number error originated from.
Comments
Post a Comment