我的 MVCContrib 网格的内容来自强类型视图上的模型。发布帖子时,当它返回控制器时,网格的内容不在模型对象中。我可以看到这是因为网格呈现为一个表格,其中包含单元格中的文本。有什么我可以做的,这样当帖子发生时,我发送到网格的列表数据会在帖子中返回?
2 回答
您可以使用 TempData 来持久化此信息服务器端。TempData 中的信息将针对一个请求持续存在。但是,我不太喜欢这个选项。
你不能从数据库中重新填充你的模型吗?如果用户没有更改信息,为什么您需要回发所有相同的未更改信息?只需从之前获得它的地方再次抓住它。
If you want to recreate the model as it was serialised into the grid, you will have to embed correctly named form elements within the grid ( or maybe outside the grid ) and within the same form as the one containing the button that is posting back to the action where you want your Model recreated.
While this is doable, you are essentially recreating __VIEWSTATE, and that defeats much of the joy of working with MVC ( read "it's an ugly hack and you should uninstall your IDE for even thinking it").
It is hard to point you in the right direction without having a better understanding of the scenario you are trying to solve. The usual workflow in these situations is get the model
- generate the page
- record any changes to the model in a form on the page
- submit the changes to an action
- get the model again
- use TryUpdate to persist the changes from the post into the model
If you are suffering performance issues ( you have proved you've got a perf problem right? You aren't optimising prematurely?), address them where they occur ( i.e. caching in your data access ), rather than bending MVC in ways it really shouldn't be.