MVC - 使用 C# 将 ViewModel 传递给 View
我有一个这样的 ViewModel:
public class EmployeeViewModel
{
Employees employee{ get; set; }
Budget budget { get; set; }
}
我喜欢将此视图模型传递给执行需要两个表的创建的视图。
我尝试在控制器中执行以下操作,将信息传递到视图,但没有成功:
EmployeeViewModel pvm = new EmployeeViewModel()
return View(pvm);
原因是员工和预算的值都为空。 如何将此信息传递给视图,以便它了解这两个表?
I have a ViewModel as such:
public class EmployeeViewModel
{
Employees employee{ get; set; }
Budget budget { get; set; }
}
I like to pass this view model to a view that does a create that requires both of the tables.
I tried doing the following in the controller that would pass information to the view but was not successful:
EmployeeViewModel pvm = new EmployeeViewModel()
return View(pvm);
The reason being is that the Value of both employee and budget it null.
How do I pass this information to the view so it knows about both of the tables?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果这样做,您将在 ViewModel 属性中获得引用:
如果员工和预算是 LINQ/ADO EF 模型,您可以将数据库中的数据附加到控制器中:
If you do this, you will have references in the ViewModel properties:
If Employees and Budgets are LINQ/ADO EF models, you could attach data from database in a controller: