0

我在这里的第一个问题,

在我的域中,我有名为

学生:

public class Student:Person
{

    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserID { get; set; }
    public string FirstName { get; set; }
    public string MiddleName { get; set; }
    public string LastName { get; set; }
    public string EmailAddress { get; set; }
    public virtual ICollection<Order> Orders { get; set; }
    public virtual Project Project { get; set; }
}

项目:

public class Project
{

    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ProjectID { get; set; }
    public int ProjectNO { get; set; }
    public string Module { get; set; }
    public string  ProjectName { get; set; }
    public string ProjectDescription { get; set; }
    public bool IsGroupProject { get; set; }
    public decimal Budget { get; set; }
    public virtual ICollection<Student> Student { get; set; }
    public virtual Supervisor Supervisor { get; set; }
}

零件:

public class Component
{

    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int ComponentID { get; set; }
    public string Name { get; set; }
    public string StockCode { get; set; }
    public string Description { get; set; }
    public decimal Price { get; set; }
    public virtual Order Order { get; set; }
}

命令:

public class Order
{


    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int OrderID { get; set; }
    public DateTime OrderDate { get; set; }
    public bool IsCollected { get; set; }
    public DateTime CollectionDate { get; set; }
    public decimal OrderTotal { get; set; }
    public virtual Student Student { get; set; }
    public virtual ICollection<Component> Components { get; set; }

    public void CheckBudget()
    {

    }

}

我有一个名为 requestcomponent 的视图模型

public class RequestComponent
{


    [Key]
    public int RequestID { get; set; }
    public Student Student { get; set; }
    public Project Project { get; set; }
    public Component Component { get; set; }
    public Order Order { get; set; }
}

我正在尝试做两件事:

1) 在视图中显示来自学生、项目、组件和订单模型的数据。2) 使用表单向视图中的学生、项目、组件和订单模型写入数据。

请你指导我。

感谢您提前提供帮助。

4

1 回答 1

0

您可以为视图创建视图模型并在控制器中准备它。我不确定您要呈现的信息的结构是什么。

假设你想在这里显示Order Information

为此,创建一个OrderViewModel并将视图特定属性放入其中。没有必要包括所有其他属性Order

public class OrderViewModel
{
    public int OrderID { get; set; }
    public DateTime OrderDate { get; set; }
    public bool IsCollected { get; set; }
    public decimal OrderTotal { get; set; }
    public Student Student { get; set; }
    //add any view specific properties, if needed like IsEditable
}

在订单控制器中

public ActionResult FindOrder(int id)
{

    Order order=DBContext.Orders.FindById(id); // get it from repository

    OrderViewModel viewModel=new OrderViewModel();
    viewModel.Id=order.Id; // etc, etc
    //Do this mapping with AutoMapper, recommended

    return View(viewModel);
}

在 Views/Order/FindOrder.cshtml

@model OrderViewModel 

<div id="order-details">

    @using (Html.BeginForm("SaveOrder", "Order", FormMethod.Post, new { @id = "form-orderInfo"}))
    {

     <label>Name</label>
     @Html.TextBoxFor(m => m.Name, new { @id = "order-name" })
     <!-- etc -->

    }
</div>


public ActionResult SaveOrder(OrderViewModel model)
{
 Order order= prepare this from OrderViewModel object. Use AutoMapper
}           

阅读AutoMapper Article

于 2013-11-01T15:30:47.083 回答