C# 使用ViewModel加载包含来自多个表的值的页面

C# 使用ViewModel加载包含来自多个表的值的页面,c#,asp.net-mvc,C#,Asp.net Mvc,我想做的是用两个不同表中的信息填充多个标签。当按下submit按钮时,它将点击my Post方法并保存用户输入的信息,并将信息自动填充到db中,但我的Get方法有问题。控制器中的Get方法如下所示: [HttpGet] public ActionResult AddOrganization(int peopleID = 1) { var peopleModel = db.People.Include("EmployeeContacts").Single(g

我想做的是用两个不同表中的信息填充多个标签。当按下submit按钮时,它将点击my Post方法并保存用户输入的信息,并将信息自动填充到db中,但我的Get方法有问题。控制器中的Get方法如下所示:

    [HttpGet]
    public ActionResult AddOrganization(int peopleID = 1)
    {
        var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID);
        return View("../Setup/AddOrganization", peopleModel);
    }
AddOrganizationViewModel vm  = new AddOrganizationViewModel();
vm.Organizations = new Organizations()
{
someOrganizationProperty = peopleModel.SomeProperty,
};
vm.People = new People()
{
somePeopleProperty = peopleModel.SomeOtherProperty,
};
//etc for the other properties and types in your viewmodel
但是,我有一个viewModel,它看起来像这样,包括此页面的Get和Post方法所需的所有表:

    public class AddOrganizationViewModel
    {
        public MusicStore.Models.Organizations Organizations { get; set; }
        public MusicStore.Models.People People { get; set; }
        public MusicStore.Models.OrganizationOptions OrganizationsOptions { get; set; }
        public MusicStore.Models.EmployeeContacts EmployeeContacts { get; set; }
    }
因此,当第一次加载视图并调用上面的Get方法时,我得到一个错误消息

The model item passed into the dictionary is of type 'System.Data.Entity.DynamicProxies.People_8080C33752A42A0C994331F5015BCCFCEB99B3ECD7AB8CA2BB11ABE67851B81B', but this dictionary requires a model item of type 'MusicStore.ViewModels.AddOrganizationViewModel'.
以下是我的看法:

<h2>AddOrganization</h2>
@model MusicStore.ViewModels.AddOrganizationViewModel

@using (Html.BeginForm("Add", "AddOrganization"))
{
<fieldset>
<legend>CONTACT INFORMATION</legend>
<div class ="editor-label">
    @Html.Label("Organization Name")
</div>
 <div class ="editor-field">
    @Html.TextBoxFor(Model => Model.Organizations.OrgName)
</div>
<div class ="editor-label">
    @Html.Label("Phone Number")
</div>
 <div class ="editor-field">
    @Html.TextBoxFor(Model => Model.Organizations.OrgPhone)
</div>
<div class ="editor-label">
    @Html.Label("Point of Contact")
</div>
 <div class ="editor-field">
    @Html.TextBoxFor(Model => Model.Organizations.OrgPointOfContact)
</div>
<div class ="editor-label">
    @Html.Label("Office Location")
</div>
 <div class ="editor-field">
    @Html.TextBoxFor(Model => Model.Organizations.OrgOfficeLocation)
</div>
</fieldset>
<fieldset>
<legend>FIRST EMPLOYEE OF ORGANIZATION</legend>
<div class ="editor-label">
    @Html.Label("Admin First Name")
</div>
 <div class ="editor-field">
    @Html.TextBoxFor(Model => Model.People.FirstName)
</div>
<div class ="editor-label">
    @Html.Label("Admin Last Name")
</div>
 <div class ="editor-field">
    @Html.TextBoxFor(Model => Model.People.LastName)
</div>
<div class ="editor-label">
    @Html.Label("Admin NID")
</div>
 <div class ="editor-field">
    @Html.TextBoxFor(Model => Model.People.NID)
</div>
<div class ="editor-label">
    @Html.Label("Admin SID")
</div>
 <div class ="editor-field">
    @Html.TextBoxFor(Model => Model.People.SID)
</div>
 <div class ="editor-label">
     @Html.Label("Admin Email")
</div>
 <div class ="editor-field">
    @Html.TextBoxFor(Model => Model.EmployeeContacts.Email)
</div>
 <div class ="editor-label">
    @Html.Label("Admin Phone Number")
</div>
 <div class ="editor-field">
    @Html.TextBoxFor(Model => Model.EmployeeContacts.PrimaryPhone)
</div>
AddOrganization
@模型MusicStore.ViewModels.AddOrganizationViewModel
@使用(Html.BeginForm(“添加”、“添加组织”))
{
联系方式
@Html.Label(“组织名称”)
@Html.TextBoxFor(Model=>Model.Organizations.OrgName)
@Html.Label(“电话号码”)
@Html.TextBoxFor(Model=>Model.Organizations.OrgPhone)
@Html.Label(“联系人”)
@Html.TextBoxFor(Model=>Model.Organizations.OrgPointOfContact)
@Html.Label(“办公地点”)
@Html.TextBoxFor(Model=>Model.Organizations.OrgOfficeLocation)
组织的第一名雇员
@Html.Label(“管理员名”)
@Html.TextBoxFor(Model=>Model.People.FirstName)
@Html.Label(“管理员姓氏”)
@Html.TextBoxFor(Model=>Model.People.LastName)
@Html.Label(“管理员NID”)
@Html.TextBoxFor(Model=>Model.People.NID)
@标签(“管理SID”)
@Html.TextBoxFor(Model=>Model.People.SID)
@Html.Label(“管理电子邮件”)
@Html.TextBoxFor(Model=>Model.EmployeeContacts.Email)
@Html.Label(“管理员电话号码”)
@Html.TextBoxFor(Model=>Model.EmployeeContacts.PrimaryPhone)

当视图只能采用ViewModel类型时,如何通过Get方法将所需信息发送到视图?是否有办法将此信息添加到我的ViewModel?如果有任何帮助或帮助,都将非常有用

 var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID);
在这种情况下,
peopleModel
的类型是什么

当您直接从数据库返回它时,看起来它还没有被“转换”或“投影”到视图所期望的实际viewmodel中

您可能需要执行以下操作:

    [HttpGet]
    public ActionResult AddOrganization(int peopleID = 1)
    {
        var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID);
        return View("../Setup/AddOrganization", peopleModel);
    }
AddOrganizationViewModel vm  = new AddOrganizationViewModel();
vm.Organizations = new Organizations()
{
someOrganizationProperty = peopleModel.SomeProperty,
};
vm.People = new People()
{
somePeopleProperty = peopleModel.SomeOtherProperty,
};
//etc for the other properties and types in your viewmodel
设置完所有数据后,可以返回
AddOrganizationViewModel

var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID);
在这种情况下,
peopleModel
的类型是什么

当您直接从数据库返回它时,看起来它还没有被“转换”或“投影”到视图所期望的实际viewmodel中

您可能需要执行以下操作:

    [HttpGet]
    public ActionResult AddOrganization(int peopleID = 1)
    {
        var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID);
        return View("../Setup/AddOrganization", peopleModel);
    }
AddOrganizationViewModel vm  = new AddOrganizationViewModel();
vm.Organizations = new Organizations()
{
someOrganizationProperty = peopleModel.SomeProperty,
};
vm.People = new People()
{
somePeopleProperty = peopleModel.SomeOtherProperty,
};
//etc for the other properties and types in your viewmodel
设置完所有数据后,可以返回
AddOrganizationViewModel

var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID);
这不会创建
AddOrganizationViewModel
对象。请在末尾添加
Select
子句,或使用该对象中的数据填充新的viewmodel


这不会创建一个
AddOrganizationViewModel
对象。要么在末尾添加一个
Select
子句,要么使用该对象中的数据来填充一个新的viewmodel。

这正是我所希望的!非常感谢您的帮助!这正是我所希望的!非常感谢您的帮助!