使用Get将ASP.Net MVC中的ViewModel从一个视图传递到另一个视图 [英] Passing ViewModel in ASP.Net MVC from a View to a different View using Get

查看:52
本文介绍了使用Get将ASP.Net MVC中的ViewModel从一个视图传递到另一个视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表视图,该列表视图具有强类型的ViewModel,其中包括我正在使用的实体列表以及我携带的其他会话类型的东西.

I have a List View which has a strongly typed ViewModel which includes the entity list I am working with together with some other session-type stuff I am carrying aruound.

在单击列表中的项目(Html.ActionLink)以转到详细信息"视图时,我可以轻松地传递实体ID.但我也想从View传递其余的ViewModel.

On clicking an item in the list (an Html.ActionLink) to go to the Details view I can easily pass the entity id. But I also want to pass the rest of the ViewModel from the View.

我可以使用各种QueryString参数构建ActionLink,然后自定义ModelBinder可以选择它们并再次为我补充ViewModel对象.但是,我不喜欢这样.

I can build the ActionLink with various QueryString parameters and then a custom ModelBinder can pick them up and hydrate the ViewModel object for me again. However, I don´t like this.

我可以使自定义ViewModel在回传到同一页等时重新水化,但是如何不使用ModelBinder并通过简单地放置ViewModel而使用GET到另一个视图将ViewModel放入Controller Action中.对象作为目标Action方法中的参数?

I can get the custom ViewModel to rehydrate when it is POSTed back to the same page etc., but how can I get the ViewModel into a Controller Action using a GET to another View without using a ModelBinder and by simply placing the ViewModel object as a parameter in the target Action method?

推荐答案

我不认为您可以做自己想做的事情,而根据我的收集,这是这样的:

I don't think you can do what you want, which from what I gather is the following:

  1. 在呈现List动作时,您想要创建到另一个动作的链接(可能在另一个控制器上,但这不是关键)

  1. While rendering the List action, you want to create a link to another action (potentially on another controller, but that's not key here)

此操作在被触发后应该可以访问首次执行ActionLink方法时存在的原始ViewModel.

This action should, when fired, have access to the original ViewModel that existed when the ActionLink method was first executed.

不幸的是,项#1和#2彼此完全断开,因此没有真正的机制将当前ViewModel传递给将在不同会话中执行的链接.

Unfortunately, items #1 and #2 are completely disconnected from each other and so there is no real mechanism to pass the current ViewModel to a link that will be executed in a different session.

这当然不是说没有解决方法:

That's not to say there aren't workarounds, of course:

您可以像这样生成操作链接:

You can generate the action link like so:

<%= 
    Html.ActionLink( 
                    "Label", 
                    "Action",  
                    "Controller",
                    new {Parameter1 = Model.Data1, Parameter2 = Model.Data2},
                    null
                   ) 
%> 

在链接的操作方法中,您可以使用传递给该操作方法的参数实例化ViewModel.

Within your linked action method, you can instantiate the ViewModel using the parameters passed to that action method.

这篇关于使用Get将ASP.Net MVC中的ViewModel从一个视图传递到另一个视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆