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

时间:2013-07-17 19:42:27

标签: c# asp.net-mvc

我想要做的是让一些标签填充来自2个不同表格的信息。按下提交按钮后,它将点击我的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);
    }

但是,我有一个看起来像这样的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>

如果只能采用ViewModel类型,如何通过Get方法将所需信息发送到视图?有没有办法可以将这些信息添加到我的ViewModel中?任何帮助或推动正确的方向都会很棒。

2 个答案:

答案 0 :(得分:1)

 var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID);

在这种情况下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

答案 1 :(得分:0)

var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID);

这不会创建AddOrganizationViewModel个对象。在末尾添加Select子句或使用该对象中的数据填充新的视图模型。