ASP.NET MVC更改模型而不影响视图 [英] ASP.NET MVC change model without affecting view

查看:65
本文介绍了ASP.NET MVC更改模型而不影响视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

先生,



我的asp.net mvc采访中有一个问题,我不知道。



问题是



我有包含2000行代码的视图在asp.net mvc中。在新的客户端请求中,我不得不更改与该视图关联的模型。所以我需要覆盖失去2000行的视图。有没有办法限制这个。所以2000行没有受到影响,模型的变化也应该反映在视图上。

Sir,

I got a question in my asp.net mvc interview which I am unaware.

The question is

I had view containing 2000 lines of code in asp.net mvc. On new client request I had to change the model associated with that view. So I need to overwrite the view loosing 2000 lines. Is there any way to restrict this. So 2000 lines doesn't got affected and also the model change should be reflected on the view too.

推荐答案

一个解决方案可能是定义一个共同的 interface 将用作基于现有模型的模型,然后新模型必须实现此接口。



这里有类似的讨论: http://stackoverflow.com/questions/17664803/interface-implementing-interface-as-a-view-model-in-asp-net-mvc- 4 [ ^ ]
One solution could be to define a common interface that will be used as model based on the existing model, then the new model must implement this interface.

There a similar discussion about this here: http://stackoverflow.com/questions/17664803/interface-implementing-interface-as-a-view-model-in-asp-net-mvc-4[^]


假设您的视图中有2000行代码。在视图中维护这些代码非常困难。您可以避免这些代码行通过viewmodel.ViewModel除了包装类之外什么,无论视图所需的数据是什么,你都可以设置为viewmodel并传递给view.So你应该为每个视图创建每个viewmodel。在这个viewmodel你可以设置所有最终视图的逻辑和数据。不需要在View中编写这些逻辑。我认为访问者期望viewmodel概念。



希望这有助于
Suppose you have 2000 lines of code in your view.It's very hard to maintain those code inside views.You can avoid those lines of code through viewmodel.ViewModel nothing but a wrapper class,whatever the data needed for the view you can set to the viewmodel and pass to the view.So you should create each viewmodel for each view.In this viewmodel you can set all the logics and data for the final view.No need to write those logics inside View.I think interviewer expect the viewmodel concept.

Hope this helps


这篇关于ASP.NET MVC更改模型而不影响视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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