1

I am very new to c# and asp.net mvc. I'm building a HR portal for our company where a user can submit a leave form among other things... So I'm using mssql as the database server and using Entity Frame work to communicate with it. I have 3 entities, user (Containing user details), permissions (the user permissions for allowing actions in the app) and then the leave form table (where the leave form details are stored). There is a one to many relationship between user - permission and then a one to many relationship between user-leave. I am not fazed about the permissions as that gets created when the user account is being created.

The problem I am facing is, how do I add a leave form for a specific user? Below is my controller code:

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Leave(MasterViewModel model)
{
    DocSubViewModel mv = model.DSModel;
    int userId = Convert.ToInt32(Session["userID"]);
    try
    {
        using (HrDcpDBContainer db = new HrDcpDBContainer())
        {
            var leave = db.leaves.Create();
            leave.dateFrom = mv.DateFrom;
            leave.dateSubmitted = DateTime.Now;
            leave.dateTo = mv.DateTo;
            leave.nrDays = mv.NrDays;
            leave.reason = mv.SpecialLeave;
            leave.TLApproval = null;
            leave.TLApprovalDate = null;
            leave.TLApprovalID = mv.TeamLeaderID;
            leave.DMApprovalDate = null;
            leave.DMApprovalID = mv.DepManagerID;
            leave.DMApproval = null;
            leave.type = mv.Type;
            leave.user = userId;
            db.leaves.Add(leave);
            db.SaveChanges();
        }
        ViewBag.Message = "Leave Form submitted Successfully. You will be redirected shortly...";
        return View("result");
    }
    catch (Exception ex)
    {
        ViewBag.Message = ex;
        //ViewBag.Message = "Leave Form submitted Successfully. You will be redirected shortly...";
        return View("result");
    }

The problem comes in leave.user = userId;. It says:

Cannot implicitly convert int to Portal.Model.user

I can't seem to find out how to do this...

3
  • it is because you're assigning userId to user. Is user an integer type? My guess is it's an object Commented Jul 25, 2017 at 12:51
  • leave.user is the primary Key in the database Commented Jul 25, 2017 at 12:51
  • Is it leave.user.userid? Commented Jul 25, 2017 at 12:52

2 Answers 2

2

You're telling it to put the UserId where your leave model is asking for a User.

Your relationship requires a User to go in there, so you'll have to update your code a little bit:

using (HrDcpDBContainer db = new HrDcpDBContainer())
{
    var leave = db.leaves.Create();
    leave.user = db.users.First(x => x.Id == userId);
}

This will put reference to the actual user in the new leave record. If you go later and check it out you'll see a column in the leave table called user_Id that has an integer value in it and is set as a foreign key to the users table.

Note that this will error if no user exists having the specified Id value. If you anticipate this to be a problem, rather use .FirstOrDefault() instead of .First() and then account for the value being null before you add it to your new leave object.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, Makes so much sense but trying to figure this out on my own is difficult. C# is not a forgiving language. Thanks for the Help!
little typo there ... x =. should be x =>
0

That's expected since User is a object and not int. What you should be doing probably is leave.user.UserId = userId; instead [Assuming leave.user is of type User which has a UserId property]

3 Comments

No assumption is necessary. OP confirmed the type of leave.user when he quoted the error message at the bottom of the question :)
@Ortund, yes but the actual assumption was that it does have a property named userid
Oh right, sorry I didn't see what you did in your answer there... Change blindness... I saw what I expected to see and I didn't expect to see you setting leave.user.UserId xD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.