TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Marius Vasile
594
1.9k
144.9k
ASP.NET Core Razor Add new data from Main page
May 24 2020 5:54 AM
I am trying to add new data and on "Save" to automatically refresh data. However, on save I get error
System.NullReferenceException: 'Object reference not set to an instance of an object.'
RoSafety.Pages.PTW.IsoC.IsoCertModel.ICData.get returned null.
If I use a separate page for Post, the data is saved but on same page as OnGet is not. How should I do it?
[BindProperty]
public
ICContent ICContent {
get
;
set
; }
public
ICData ICData {
get
;
set
; }
public
async Task<IActionResult> OnGetAsync()
{
ICData = await _context.ICDatas
.Include(a=>a.ICContents)
.AsNoTracking()
.FirstOrDefaultAsync(a => a.IdICD == 1);
if
(ICData ==
null
)
{
return
NotFound();
}
return
Page();
}
public
async Task<IActionResult> OnPostAsync()
{
if
(!ModelState.IsValid)
{
return
Page();
}
var emptyIsoCert =
new
ICContent();
if
(await TryUpdateModelAsync(emptyIsoCert,
"ICContent"
,
s => s.Equipment,
s => s.EnergSource,
s => s.IsoType,
s => s.IsoMethod,
s => s.LockNo,
s => s.PreparedBy,
s => s.ImplementedBy,
s => s.VerifiedBy,
s => s.IsoStatus ))
{
_context.ICContents.Add(emptyIsoCert);
await _context.SaveChangesAsync();
//return RedirectToPage("IsoCert");
return
await OnGetAsync();
}
return
null
;
}
Reply
Answers (
7
)
How to create crystal report in asp.net mvc using c#?
How to resolve nuget command issue group-policy?