1

我想要做的是让许多标签填充来自 2 个不同表的信息。当按下提交按钮时,它会点击我的 Post 方法并保存用户输入的信息并将信息自动填充到数据库中,但是我的 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 中?任何朝着正确方向的帮助或推动都会很棒。

4

2 回答 2

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.

于 2013-07-17T19:47:50.063 回答
0
var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID);

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

于 2013-07-17T19:48:03.040 回答