0

我有一个像这样的 2 个样本模型(我已经输入了我只想要的字段)

[Table("TripAllocation", Schema = "dbo")]
public class TripAllocation
{
    public int TripAllocationId { get; set; }
    public ICollection<Requisition> Requisitions { get; set; }

}


[Table("Requisition", Schema = "dbo")]
public class Requisition
{
    [Key]
    public int RequisitionId { get; set; }

    public int? TripAllocationId { get; set; }
    public TripAllocation TripAllocation { get; set; }

}

所以现在我想通过 TripAllocation 表在 Requisition 表中选择 TripAllocationId 的值。我不想通过在申请视图中创建 driodownlist 来做到这一点。

我想从 TripAllocation 表中执行此操作,并添加 TripAllocationId 的申请列表。

那么如何通过使用 MVC 处理控制器和视图来做到这一点。

(当我创建旅行时,应将申请清单应用于一个 AllocationTripId)

4

2 回答 2

0

这基本上就是我想要的。当我通过旅行分配创建旅行分配时。我想为一个原始的 TripAllocation 添加一些申请列表。(但我可以在创建申请时简单地做,我可以通过在申请创建视图中创建一个下拉列表来为申请的一个瞬间添加一个 TripAllocation。但这不是我想要的。我想做与此相反的操作)

于 2012-08-08T03:58:28.123 回答
0

I don't know if I fully understand your question, but it sounds like you want your view to display a list of requisitions searching by a TripAllocationId?

First, an assumption of mine based on your code sample, is that you're using LINQ-to-SQL which I haven't used for quite some time so forgive me if my code sample isn't 100% complete. But basically, you just need to use the DataContext to eager load the Requisitions on a TripAllocation. Your controller method would look something like this:

public ActionResult Requisitions(string id)
{
    TripAllocation allocation;
    List<Requisition> requisitions;

    using (var context = new MyDataContext)
    {
        allocation = context.TripAllocations.SingleOrDefault(c => c.TripAllocationId == id);
        requisitions = allocation.Requisitions.ToList();
    }

    return View(requisitions);
}

If I've understood your question correctly, you'll pass your controller method a TripAllocationId and it will use LINQ-to-SQL to navigate the object graph and return to the view a List<Requisition>.

于 2012-08-07T16:03:48.610 回答