ASP.NET-MVC – Strong Type Object Hold Controller and View Separation

I have a question about keeping the controller and the view separate. In my opinion, the controller should only pass the model to the view, and the view decides how to display the model. In this way, the controller and the view The model remains independent and can be developed independently. However, I have seen many tutorials on the Internet, and even in the book Pro ASP.NET MVC Framework, I have seen many examples of using ViewData [“string”] or TempData [“string”] .

Will this introduce two problems? The first is that the view is now a bit coupled to the controller because it has to know the name of the string that the controller sets in ViewData/TempData. The second is that these are loosely typed, which means there is no Intellisense. If I am developing Controller, I can’t tell another developer working on the view to only use Intellisense as the model, I have to give him the name of the string, if I change the string name, I also have to change it in the view.

I think the final question I have to ask is, is this correct? Or what am I not understanding?

Viewing data is a way to pass information between the view and the controller, but as you That said, there is no wisdom, it increases coupling. Instead, you should consider using ViewModel. For the way to use ViewModel and the advantages and disadvantages of ViewDate and ViewModel, please refer to the Scott Gu NerdDinner example (available for free).

I hope it will help.

I have a question about keeping the controller and the view separate. In my opinion, the controller should only be the model Passed to the view, the view decides how to display the model. In this way, the controller and the model remain independent and can be developed independently. However, I have seen many tutorials on the Internet, and even in the book Pro ASP.NET MVC Framework, I have seen many uses Examples of ViewData [“string”] or TempData [“string”].

Will this introduce two problems? The first is that the view is now a bit coupled to the controller because it has to know the name of the string that the controller sets in ViewData/TempData. The second is that these are loosely typed, which means there is no Intellisense. If I am developing Controller, I can’t tell another developer working on the view to only use Intellisense as the model, I have to give him the name of the string, if I change the string name, I also have to change it in the view.

I think the final question I have to ask is, is this correct? Or what am I not understanding?

Viewing data is a way to pass information between the view and the controller, but as you said, without wisdom, it increases coupling. On the contrary. , You should consider using ViewModel. For the way to use ViewModel and the advantages and disadvantages of ViewDate and ViewModel, please refer to Scott Gu NerdDinner example (available for free).

I hope it will help.< /p>

Leave a Comment

Your email address will not be published.