Home > OS >  ASP.NET MVC Checking a value in a table on a controller
ASP.NET MVC Checking a value in a table on a controller

Time:06-27

In a controller I want to redirect to a view depending on a result of a if statement. I need to check if a value of an UserID on table1 is the same on a column of the table2, but the variable "vacon" assumes a query expression instead of the query value:

public ActionResult EditDriver ()
{
     using (Volta12BaseDEntities bd = new Volta12BaseDEntities())
     {
          var actualuser = User.Identity.GetUserId();
          var vacon = bd.table2.Where( m => m.UserID == actualuser).ToString();

          if (actualuser == vacon)
          {
              return View()
          }
          else 
              return Redirect to Action("AddDriver");
      }
}

(The var actualuser is getting the result I want). I just wanted the var vacon to assume the same value of the other variable if it already exists in the table 2.

CodePudding user response:

Change it a bit differently

var vacon = bd.table2.FirstOrDefault(m => m.UserID == actualuser);
      if (vacon != null)

and return your view.

Also, I would recommend to pay attention to variable names. Your actualuser should be actualUserId - because you do not return user object, but user's id.

UPDATE: as per suggestion from @Qwertyluk, there is no need to have Where method.

  • Related