Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would avoid doing everything with model. Model is for data transaction and needs to aways reflect your database. </p> <p>You have two things there. -One is the Model that you can use to store your data in its primitive state -The other is the view where you need to show that information</p> <p>So, what to do?? Simple, first do de model and make a viewModel. The viewModel will reflect the view and its rules, so each view and partial view will have one. Making it simple use your model in the way its writen up there but take off the things that are from view (exe: [Required(ErrorMessage = "Last Name is required")]) for that you will make another class to hold that stuff, for example, you can make a view where 2 types of users are presente, for that you can make something like this:</p> <pre><code>public class CompanyView { private company= new Company(); private contractor = new Contractor(); [Display(Name = "Contractor Name")] public string ContractorName{ get{return contractor.FisrtName+ contractor.LastName;} } public int CompanyID { get{return company.CompanyID;} set{company.CompanyID = value;}} [Required(ErrorMessage = "Name of the Company required")] public string CompanyName { get{return company.CompanyName;} set{company.CompanyName = value;}} private List&lt;Offer&gt; offers; public SelectList Offers{ get { return new SelectList(offers, "OfferID", "OfferName", ""); } } } </code></pre> <p>Or something like that. The point is that you can now create a class that represents the view that not aways is exactly the same as you see in the database. Doing this you can extend the view and make something that uses your models to make something showable to the user and auto-filled. For example, when the user post his form with the view info that will return the ViewModel alredy filled. Them you can use your alredy filled models that are inside your viewmodel as private to save the anything that you need.</p> <p>Note that i note used all contractor fields in the viewmodel. Imagine a case where you just need to show some contractor name when you are changing the company details. In that case youre Not going to save anything from contractor, you just need to show his name and in the viewmodel you just need to put his name for showing and you dont need the required validation and etc. Just show it is enouth. The data will come from a query that you can fill it in the constructor, factory or anything that you want when you create the viewModel.</p> <p>It is confusing, but i hope it helps, if you not understand something i try to makeit better later. Sorry for the "so so" english too</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload