Is there a function in my controller to check if the user email is active or not?
public async Task<IActionResult>ActiveEmailAccount(EmailActiveAccountViewModel active)
{
if (ModelState.IsValid)
{
var result = await _userService.ActiveAccount(active);
switch (result)
{
case ActiveEmailResult.Error:
ModelState.AddModelError("email", "error");
break;
case ActiveEmailResult.NotActive:
ModelState.AddModelError("email", ""NotActive");
break;
case ActiveEmailResult.Success:
ModelState.AddModelError("email", "active");
break;
}
ViewData["active"] = result;
return View(active);
}
}
This is my repository code:
public async Task<User> GetUserByActiveCode(string activeCode)
{
return await _context.Users.FirstOrDefaultAsync(u => u.EmailActiveCode == activeCode);
}
public async Task<bool> CheckEmailActiveCode(string activeCode)
{
return await _context.Users.AnyAsync(u => u.EmailActiveCode == activeCode);
}
In the service, I utilize these repository functions:
public async Task<User> GetUserByActiveCode(string activeCode)
{
return await _userRepository.GetUserByActiveCode(activeCode);
}
public async Task<ActiveEmailResult> ActiveAccount(EmailActiveAccountViewModel active)
{
var activeEmailExist=await _userRepository.CheckEmailActiveCode(active.EmailActiveCode);
var user = await _userRepository.GetUserByActiveCode(active.EmailActiveCode);
if (activeEmailExist== null )
return ActiveEmailResult.Error;
if (activeEmailExist)
{
user.UserState = UserState.Active;
_userRepository.UpdateUser(user);
await _userRepository.SaveChange();
return ActiveEmailResult.Success;
}
return ActiveEmailResult.NotActive;
}
I receive the active code as a view model in the controller:
public class EmailActiveAccountViewModel
{
public string EmailActiveCode { get; set; }
}
public enum ActiveEmailResult
{
NotActive,
Success,
Error,
}
However, when checking and tracing it with breakpoint, it returns only null. For example, from this URL:
https://localhost:44385/account/ActiveEmailAccount/ddd8915deba74659be3ca89fdc118f14
Why does it return null instead of accepting "ddd8915deba74659be3ca89fdc118f14"?
In the view, I use ViewData to check it and have @model of EmailActiveAccountViewModel:
<div class="alert alert-success"> @ViewData["Active"] </div>
/////Edited////
How can I display these results in separate P tags in the view? Should I use an if statement for that?