简体   繁体   English

MVC在一个视图中保存来自两个模型的数据

[英]MVC Save data from two models in one view

Hi I have spent hours trying to work out how to do this one. 嗨,我已经花了数小时试图找出如何做到这一点。

So lets say I have two models: 所以可以说我有两个模型:

model 1 - AccountInformation 模型1-AccountInformation

model 2 - AccountHoldersImages 模型2-AccountHoldersImages

One account holder can have many images. 一个帐户持有人可以拥有许多图像。 I am trying to achieve on the accountInformation create view (using Scaffolding) to include the facility to add the images at the same time of creation of the user account. 我试图在accountInformation创建视图(使用脚手架)上实现,以包括在创建用户帐户的同时添加图像的功能。 Is this possible? 这可能吗?

One account holder can have many images 一个帐户持有人可以拥有许多图像

In which case, AccountHoldersImages must be a part of AccountInformation. 在这种情况下,AccountHoldersImages必须是AccountInformation的一部分。

If in different views you are using AccountHoldersImages separately, then may be you can keep the view models separate. 如果在不同的视图中分别使用AccountHoldersImages,则可以将视图模型分开。

However, for this particular view that you are talking about, you will have to wrap the view models into a single view model as MelancialUK commented above. 但是,对于您正在谈论的这个特定视图,您将必须将视图模型包装为单个视图模型,如上面的MelancialUK所述。

Another way is to pass the models as part of ViewData[]. 另一种方法是将模型作为ViewData []的一部分传递。 If it is a form, then on post you can use FormCollection parameter of your [HttpPost] action to get your images. 如果是表单,则可以在发布时使用[HttpPost]操作的FormCollection参数来获取图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM