c# - Value cannot be null. Parameter name: entity -
this question has answer here:
i have following view (basic "edit" template)
@model successstories.models.testimonials @using (html.beginform()) { @html.antiforgerytoken() <div class="form-horizontal"> <h4>testimonials</h4> <hr /> @html.validationsummary(true, "", new { @class = "text-danger" }) @html.hiddenfor(model => model.id) <div class="form-group"> @html.labelfor(model => model.testimonial, htmlattributes: new { @class = "control-label col-md-2" }) <div class="col-md-10"> @html.editorfor(model => model.testimonial, new { htmlattributes = new { @class = "form-control" } }) @html.validationmessagefor(model => model.testimonial, "", new { @class = "text-danger" }) </div> </div> <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>
and following actionresult in controller:
[httpget] public actionresult edit(int id) { testimonialscontext testcontext = new testimonialscontext(); testimonials testimonials = testcontext.testimonialcontext.find(id); return view(testimonials); } [httppost] public actionresult edit(testimonials testimonial) { testimonialscontext testcontext = new testimonialscontext(); testcontext.entry(testimonial).state = entitystate.modified; testcontext.savechanges(); return redirecttoaction("index"); }
the error on line:
testcontext.entry(testimonial).state = entitystate.modified;
the error "value cannot null. parameter name: entity
description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code.
exception details: system.argumentnullexception: value cannot null. parameter name: entity"
please help. i've looked can't find solution work me. thanks!
thanks everyone. figured out way fix it, based on telling me null.
[httppost, actionname("edit")] public actionresult editconfirmed(int id, string testimonial) { testimonialscontext testcontext = new testimonialscontext(); testimonials testimonial = testcontext.testimonialcontext.find(id); testimonial.testimonial = testimonial; testcontext.entry(testimonial).state = entitystate.modified; testcontext.savechanges(); return redirecttoaction("index"); }
testimonial name of input box, same name entry in database table.
Comments
Post a Comment