I have the following:
public Class Vacancy{
public int VacancyID {get;set;}
public List<Application> Applications {get;set;}
}
public Class Applicant{
public int ApllicantID {get;set;}
public List<Application> Applications {get;set;}
}
public Class Application{
public int ApplicationID {get;set;}
public int VacancyID {get;set;}
public int ApplicantID {get;set;}
public virtual Applicant Applicant {get;set;}
public virtual Vacancy Vacancy {get;set;}
}
then i created a control on Vacancy Model.
what i want to do:
1) view all vacancies
2) when a vacancy selected i wanna show its list of application in the same page
3) when an application is selected from the previous step i wanna get the applicant details
i tried to use this tutorial
http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc-application
but i’m going no where with it, i hit a wall and i’m so confused atm
I am pretty sure that you don’t want anyone here to code it from A-Z. So here i am giving you some starting. The half part of the solution i am providing and leaving you to do the rest yourself.
First of All you have some Spelling issues in your code. Class keyword should be lowercase. And you should use the same IDs for creating the foreign key reference. (If you have ApplicantId in your Application Class as your Primary Key, you should use the same spelling in your Applications class. Entity framework code-first creates the foreign key relation when it sees the same name like that).
Assuming you have a
DBContextclass like thisTo List All Vacancies, Create an Action called “Index”
So we should have a View for this action where we need to display all the Vacancies. So add an Index View which is strongly typed to a collection of Vacancy Model like this
In this view we included a reference for
jQuerylibrary which we will be using to make some ajax calls. We need to to use ajax show the Application information for a selected vacancy in the same page. for this we will make anasynchronousrequest to another action calledGetApplicationsinside our Job controller with the vacancy id as the parameter. We are simply looping thru all available Vacancies and creating an Anchor tag for that here.Go back to Job controller and create an Action method called
GetApplicationslike this.That is pretty clear to understand , We are querying to get all applications for the selected Vacancy, Return that to a View. So we need to create a view called
GetApplications.cshtmlwith the below content.Straight forward ! Simply printing the result in a loop.
That is it. It should work. Whenever you click on the vacancy link, it will make a call to the GetApplications method with the id as parameter and that action method will return a view with HTML markup which listss all applications for that vacancy id.
Once you do this, you should be able to create the second part yourself. It is the same logic. You may need to create a similar GetApplicants action method which returns data.
Good Luck
Note : Use
Firebug/fiddlerto see what (ajax) requests are going to the action methods with what parameter. This will help you to understand how it works.