Answer the question
In order to leave comments, you need to log in
View model how to organize correctly?
The question is how to properly organize models in mvc.
1.Question
For example, there is a product creation page:
public class ProductCreateViewModel
{
[Required]
[Display(Name = "Наименование продукта")]
public string Name { get; set; }
//... еще свойства
}
public class ProductEditViewModel
{
public int Id { get; set; }
[Required]
[Display(Name = "Наименование продукта")]
public string Name { get; set; }
//... еще свойства
}
public class ProductListViewModel
{
List<SectionTdo> Sections {get; set;}
}
public class SectionTdo
{
public int Id { get; set; }
public string Name { get; set; }
List<ProductTdo> Products
}
public class ProductTdo
{
public int Id { get; set; }
public string Name { get; set; }
}
Answer the question
In order to leave comments, you need to log in
Good afternoon everyone! Hmm, what's wrong with inheritance? Make a base view model - BaseViewModel, put all the basic properties there. If you need some properties for Add and others for Edit, just put some in AddViewModel and others in EditViewModel. At the same time, common properties like Id, Name, etc. will also be available to you in both ViewModels, because both AddViewModel and EditViewModel will inherit from BaseViewModel.
In defense of such a decision, I can say the following:
1. All common properties are taken out separately. They are available at any time. If you change common properties , you can edit them in one place .
2. Specialproperties that are needed only for a specific situation (Add, Edit, Delete, ...) can be moved to a special view model.
Why do you complicate your life by duplicating pieces of code? I think there is no need to explain what it threatens in case of changes or application scalability ...
Regarding the choice between model and view : after developing on WPF using the MVVM pattern, I personally use ViewModel in MVC. This allows you to further separate the data from the presentation, adding, as they say in a scientific way, an additional level of abstraction. So here I also fully support you.
Good luck!
You have bad coding style. Name the variables something more descriptive, without ambiguity ProductListViewModel is a Model (Model) or a View (View).
one)
yes, but you don't need to clone the properties, just declare an instance of the class, or inherit
public class ProductCreateViewModel {
public string Name { get; set; }
//... еще свойства
}
public class ProductEditViewModel {
public int Id { get; set; }
public ProductCreateViewModel productCreate
//... еще свойства
private static void CreateNewProduct(){
// здесь обрабатываете состояние
}
}
public class milk{
public string nameSection { get; set; }
}
public class cheese : milk{
public string name { get; set; }
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question