[RESOLVED]How to call Getmethod in HomeController?

Hi Friends,

              I have using Three Layers in my Project .Namely DataAccessLayer ,BusinessLayer and CommonEntity Layer.

When i am trying to  edit the Record from view . I need to write the Getmethod .I created but my doubt is , how to call that method in HomeController .

I show the code :  Help me out if possible ..

DataLayerFuntion: 

public CrickeCommontEntity GetCrickById(int PlayerId)
{

CrickeCommontEntity Objds = new CrickeCommontEntity();

using(CRICKETEntities Context =new CRICKETEntities())
{
var Temp = Context.PLARYERHISTORies.Where(m => m.PLAYER_ID == PlayerId).SingleOrDefault();

Objds.Common_Player_Id=Temp.PLAYER_ID;
Objds.Common_Country = Temp.COUNTRY;
Objds.Common_Player_Name = Temp.PLAYER_NAME;
Objds.Common_Total_Runs = Temp.TOTAL_RUNS;
Objds.Common_Total_Wickets = Temp.TOTAL_WICKETS;
Objds.Common_Player_Rank = Temp.PLAYER_RANK;
Objds.Common_Catches = Temp.CATCHES;
Objds.Common_Player_Role = Temp.PLAYER_ROLE;
Objds.Common_Play_For = Temp.PLAY_FOR;

};

return Objds;

}
}

BusinessLayer:

public CrickeCommontEntity GetCrickByIdBL(int PlayerId)
{
ICricketDataLayer DbObj = new CricketDataLayer();
return DbObj.GetCrickById(PlayerId);

}

HomeController :

public ActionResult Edit(int id)
{

ICricketInteraction ObjDB=new CricketInteraction();

IList<CricketViewModel> _EditView = new List<CricketViewModel>();

return View(ObjDB.GetCrickByIdBL(id));
}

       The above homecontroller functions is wrong ,i require the solution .

Thx in advance 

-Karthik k 

 

            

Hi Karthik,

You can instantiate the BusinessLayer Class and call the method by passing the id.

public ActionResult Edit(int id)
{

IList<CricketViewModel> model = new List<CricketViewModel>();
IBusinessLayerService service= new BusinessLayerService();  //// Instead directly Instantiating this, you can also use // Dependency Injection

model  = service.GetCrickByIdBL(id);

return View(model);
}

@ Rajesh ,

           The same i called, as you mentioned .If i do so , i gets the error Typecast error .How to do that 

Thanks in Advance ,

Karthik k

 

You are getting typecasting error because your function GetCrickByIdBL(int PlayerId) returns
CrickeCommontEntity but your are trying to fetch the data into object of IList<CricketViewModel>. That’s wrong.

@ PawanPal,

                        I thought so .But i want solution .How to get rid of that issue. 

Thanks ,

Karthik k

 

Hi,

Change Database Layer Method implementation to return different object as:

public CricketDTO GetCrickById(int PlayerId)
{

CricketDTO Obj = new CricketDTO();
....

This class will contain the properties which you want to display in UI.(called as VIewModel)

You will have to change BusinessLayer Method return type from ENtity to this new Class.

Leave a Reply