简体   繁体   中英

ASP.NET MVC Checking a value in a table on a controller

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM