-1

我有以下内容:

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;}
 }

然后我在空缺模型上创建了一个控件。我想要做什么:1)查看所有空缺职位 2)当一个空缺职位被选中时,我想在同一页面中显示其申请列表 3)当从上一步中选择一个申请时,我想获取申请人的详细信息我试图使用这个教程 http://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/reading-related-data-with-the-entity-framework-in-an-asp-net-mvc -应用程序,但我无处可去,我撞到了墙,我很困惑atm

4

1 回答 1

3

我很确定您不希望这里的任何人从 AZ 对其进行编码。所以在这里我给你一些开始。我提供的解决方案的一半,让你自己做剩下的。

首先,您的代码中有一些拼写问题。类关键字应为小写。您应该使用相同的 ID 来创建外键引用。(如果您的应用程序类中有申请人 ID 作为主键,您应该在应用程序类中使用相同的拼写。实体框架代码在看到类似的名称时首先创建外键关系)。

假设您有这样的DBContext课程

public class YourDBContext:DbContext
{
    public DbSet<EFCodeFirst.Models.Vacancy> Vacancies { set; get; }
    public DbSet<EFCodeFirst.Models.Applicant> Applicants { set; get; }
    public DbSet<EFCodeFirst.Models.Application> Applications { set; get; }
}

要列出所有空缺,请创建一个名为“索引”的操作

public ActionResult Index()
{
   YourDBContext db = new YourDBContext();
   var allVacancies = db.Vacancies.ToList();
   return View(allVacancies);
}

所以我们应该有这个动作的视图,我们需要在其中显示所有的空缺。因此,将强类型的索引视图添加到这样的空缺模型集合中

@model IEnumerable<EFCodeFirst.Models.Vacancy>

<h2> All Vacancies </h2>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<div id="divVacancies">
@foreach (var vacancy in Model)
{
  <p> @Html.ActionLink(vacancy.VacancyID.ToString(), "GetApplications","Job", new { @id = vacancy.VacancyID }, new {  @class = "ajaxLink" })</p>
}
</div>

<div id="divApplications"></div>

<script type="text/javascript">
    $(function () {
        $(".ajaxLink").click(function (e) {
            e.preventDefault();
            var target = $(this).attr("href");
            $("#divApplications").load(target);
        });
     });
</script>

在这个视图中,我们包含了一个jQuery库引用,我们将使用它来进行一些 ajax 调用。我们需要使用 ajax 在同一页面中显示所选空缺的申请信息。为此,我们将向Job 控制器内部asynchronous调用的另一个操作发出请求GetApplications,并将空缺 id 作为参数。我们只是循环遍历所有可用的空缺并在这里为它创建一个锚标记。

回到 Job 控制器并创建一个GetApplications像这样调用的 Action 方法。

    public ActionResult GetApplications(int id)
    {
        SampleContext db = new SampleContext();
        var allApplications = db.Applications.Where(x => x.VacancyID == id).ToList();
        return View(allApplications);
    }

这很容易理解,我们正在查询以获取所选空缺的所有申请,然后将其返回到视图。所以我们需要创建一个名为GetApplications.cshtml以下内​​容的视图。

@model IEnumerable<EFCodeFirst.Models.Application>
<h2>Applications </h2>
@foreach (var application in Model)
{
 <p> @Html.ActionLink(application.ApplicantID.ToString(), "GetApplicants", new { @id = application.VacancyID, @class = "ajaxLink" })</p>
}

直线前进!只需在循环中打印结果。

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 /fiddler to see what (ajax) requests are going to the action methods with what parameter. This will help you to understand how it works.

于 2012-05-10T16:09:41.277 回答