jeudi 8 mars 2018

How to write an if statement to check if there is a Applicant ID associated with a User ID in .NET Entity Framevwork MVC

I've managed to set up a method in my Account Controller that takes the id associated with a users login and pushes it to my Get_Profile method in another controller.

public ActionResult CheckProfile()
    {
        string userId = User.Identity.GetUserId();
        return RedirectToAction("GetNAA_Profile2", new { UserId = userId, Controller = "NAAProfile" });
    }

The GetNAA_Profile2 method is defined like this within the DAO section of the Data layer.

public NAA_Profile GetNAA_Profile2(string UserId)
    {
        IQueryable<NAA_Profile> _profiles;
        _profiles = from profile
                        in _context.NAA_Profile
                    where profile.UserId == UserId
                    select profile;


        return _profiles.ToList<NAA_Profile>().First();

    }

It pulls up the associating profile matching up with the UserId from the Profile table in a database I have set up in a database.

CREATE TABLE [b].[NAA_Profile] (
[ApplicantId]      INT            IDENTITY (1, 1) NOT NULL,
[ApplicantName]    NVARCHAR (50)  NOT NULL,
[ApplicantAddress] NVARCHAR (50)  NOT NULL,
[Phone]            NVARCHAR (50)  NOT NULL,
[UserId]           NVARCHAR (128) NULL,
PRIMARY KEY CLUSTERED ([ApplicantId] ASC)

);

The method is then finally executed in the Profile controller returning a view with the passed UserId.

 public ActionResult GetNAA_Profile2(string UserId)
    {
        return View(_NAAService.GetNAA_Profile2(UserId));
    }

My main question is how can I implement an if statement in my method to check if there is a profile associated with the UserId and then redirect the user to another method if there isn't any matching rows.

Aucun commentaire:

Enregistrer un commentaire