Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

NullReferenceException when posting (razor pages)

$
0
0
<div>

I have a small application for arranging events, and wants have a page for sending emails or sms to all participants. Have this code behind:


public class MessagingModel : PageModel { private readonly IMessageLogService _messageLogService; private readonly IEventInfoService _eventinfos; private readonly IRegistrationService _registrationService; private readonly RegistrationEmailSender _registrationEmailSender; [BindProperty] public InputModel Input { get; set; } [TempData] public string StatusMessage { get; set; } public class InputModel { public string EmailSubject {get; set;} public string EmailContent { get; set; } public string SmsContent { get; set; } } public MessagingModel( IMessageLogService messageLogService, IEventInfoService eventinfos, IRegistrationService registrationService, RegistrationEmailSender registrationEmailSender) { _messageLogService = messageLogService; _eventinfos = eventinfos; _registrationService = registrationService; _registrationEmailSender = registrationEmailSender; } public EventInfo EventInfo {get;set;} public async Task<IActionResult> OnGetAsync(int id) { EventInfo = await _eventinfos.GetAsync(id); return Page(); } public async Task<IActionResult> OnPostSendEmailAsync(int id) { if (!ModelState.IsValid) { return Page(); } // Send emails StatusMessage = "Email sent"; EventInfo = await _eventinfos.GetAsync(id); return RedirectToPage(); } public async Task<IActionResult> OnPostSendSMSAsync(int id) { if (!ModelState.IsValid) { return Page(); } // Send SMS StatusMessage = "SMS sent"; EventInfo = await _eventinfos.GetAsync(id); return RedirectToPage(); } } }
</div> <div>However I get: </div> <div>
NullReferenceException: Object reference not set to an instance of an object.
losol.EventManagement.Pages.Admin.Events.Messaging_Page+<ExecuteAsync>d__20.MoveNext() in Messaging.cshtml+
    ViewData["Title"] = "Meldinger – " + @Model.EventInfo.Title;
So it seems that the EventInfo is not loaded after posting the page. How do I fix it?</div>

Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>