Thursday, July 28, 2016

Renaming classes in ASP.NET MVC with Visual Studio

One of the things in Visual Studio is that it makes it relatively easy to rename classes in C# using the "Rename" command.  Visual Studio will then attempt to apply this rename operation throughout the project to ensure all references are also renamed.

However, one area where Visual Studio fails is in ASP.NET MVC Razor Views.

For example, if you have a binding to a Model class such as this:

@model List<MyModel>

and you then change the name of the Model class to MyViewModel, the C# code will be updated, but the Razor View will not subsequently be updated!

Therefore, you will still have to manually update the reference in the Razor View to match your new Model class name such as:

@model List<MyViewModel>

Just something to watch out for as you perform your refactoring and renaming within ASP.NET MVC!!

No comments:

Post a Comment