我的第一个问题,
在我的域名中,我有一个名为
的课程学生:
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)在视图中使用表格向学生,项目,组件和订单模型写入数据。
请你指导我。
先谢谢你的帮助。
答案 0 :(得分: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
}