Reputation: 254
I'm referencing two models in one controller. Can I combine the models into one .cs file or do they need to be separate? It just seems that I could keep things cleaner by putting them in one file.
Upvotes: 1
Views: 2233
Reputation: 2356
Yes, of course! You can create one file with several models. It's usually used for models, which refer to the same entity or controller. The AccountModels.cs
file example:
using System;
namespace SolutionName.Web.Models
{
public class LogOnModel { /*parameters*/ }
public class RegisterModel { /*parameters*/ }
public class ChangePasswordModel { /*parameters*/ }
}
And sometimes you can create one model inside other. You can do this when you have a little model, which depends on other one and using only for the "paren" model.
For example:
using System;
namespace SolutionName.Web.Models
{
public class StartPageViewModel
{
public HeaderViewModel Header { get; set; }
public BodyViewModel Header { get; set; }
public FooterViewModel Header { get; set; }
/*parameters*/
class HeaderViewModel { /*parameters*/ }
class BodyViewModel { /*parameters*/ }
class FooterViewModel { /*parameters*/ }
}
}
Upvotes: 2