I am refactoring a web app built with previous versions of Asp.Net MVC where all the views have 2 codebehind files each.
Also if the views are working correctly I want to begin to remove all the code behind files as most of them are empty.
There are few of them that have properties like this in the codebehind:
public partial class List {
public Message NewMessage { get { return new Message(); } }
}
Before completely removing them (I know this methods do not pertain to the views) how do I replicate them in the single page aspx or ascx?
I just tried to copy and paste the public methods in <% %> tags but this is not working.
May I ask why you were using this approach in the first place? I think you could achieve the same thing by making your
Viewstrongly typed, and pass the message as theView‘sModel.If you already have a strongly typed
View, you could make a custom class in your class library that has room for your message, for exampleYou can of course make that type generic as well, allowing for a
ModelWithMessage<T>construct and avoiding having to cast theModelto whatever type you need.Another way to do it would be to use
TempData. In your Controller, setThen in your
Viewyou write the message out simply withWhich approach you choose depends on what your Message class contains.