asp.net mvc - many to one Find(id) entity framework MVC 4 -


i have 1 many relationship between table userprofiles , companies in details action method i'm trying username have company userprofile userid display company owner using find(id) method not working.

the details action method

    public actionresult details(int id = 0)     {         var companys = db.companys.include(c => c.userprofile.userid);         var company = companys.find(id);         if (company == null)         {             return httpnotfound();         }         return view(company);     } 

so i'm doing wrong please correct me , help.

and models company

 public class company {     [scaffoldcolumn(false)]     public int companyid { get; set; }      [required(errormessage = "company name required")]     [displayname("company name")]     [stringlength(40)]     public string companyname { get; set; }      [required(errormessage = "mobile number required")]     [displayname("mobile number")]     //[datatype(datatype.phonenumber)]     public string companyphonenumber { get; set; }      [required(errormessage = "company address required")]     [displayname("company address")]     [stringlength(128)]     public string companyaddress { get; set; }      //must change in view(linq) give manager user     [required(errormessage = "manager name required")]     [displayname("manager name")]     public int userid { get; set; }     public userprofile userprofile { get; set; }      public icollection<store> stores { get; set; }     public icollection<product> products { get; set; } } 

}

and userprofile model

 [table("userprofile")] public class userprofile {      [key]     [databasegeneratedattribute(databasegeneratedoption.identity)]     public int userid { get; set; }     public string username { get; set; }     public icollection<usersinroles> usersinroles { get; set; }     public membership membership { get; set; }     public icollection<company> companys { get; set; }     public icollection<comment> comments { get; set; }     public icollection<like> likes { get; set; }  }  } 

try :

 public actionresult details(int id = 0)     {         var companys = db.companys.where(x => x.companyid == id).tolist();         if (companys == null)         {             return httpnotfound();         }         return view(companys);     } 

Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

inno setup - TLabel or TNewStaticText - change .Font.Style on Focus like Cursor changes with .Cursor -