使用ViewModel从多个表加载具有值的页面
本文关键字:加载 ViewModel 使用 | 更新日期: 2023-09-27 18:23:50
我想做的是用来自两个不同表的信息填充多个标签。当按下提交按钮时,它会点击我的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中?朝着正确的方向提供任何帮助或推动都会很好。
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
。
var peopleModel = db.People.Include("EmployeeContacts").Single(g => g.PeopleID == peopleID);
这不会创建AddOrganizationViewModel
对象。在末尾添加Select
子句,或者使用该对象中的数据来填充新的视图模型。