Home > other >  Data return null from view to controller in ASP.NET Core
Data return null from view to controller in ASP.NET Core

Time:09-16

I have a function in my controller for check user email that 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);
    }

and this is my repository to get it:

  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);
         
    }

and in service I get it from service:

      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 active code in controller as a view model:

 public class EmailActiveAccountViewModel
{ 
    public string EmailActiveCode { get; set; }

}

public enum ActiveEmailResult
{
    NotActive,
    Success,
    Error,
}

and when I want to check it and trace it with break point , it will return just null for example this code from database:

https://localhost:44385/account/ActiveEmailAccount/ddd8915deba74659be3ca89fdc118f14

why it returns just null and doesnot accept :ddd8915deba74659be3ca89fdc118f14

in view I write just view data for check it and has @model of EmailActiveAccountViewModel

<div > @ViewData["Active"] </div>

CodePudding user response:

You send the request:https://localhost:44385/account/ActiveEmailAccount/ddd8915deba74659be3ca89fdc118f14 to the ActiveEmailAccount action, in this case you can not receive the value of EmailActiveAccountViewModel model.

You need send the request like: https://localhost:44385/account/ActiveEmailAccount?EmailActiveCode =ddd8915deba74659be3ca89fdc118f14.

Result:

enter image description here

  • Related