in Education by
I'm using ASP.NET MVC3 and i'm wondering that the default modelbinder binds to public properties but not to public fields. Normally i just define the model classes with properties but sometimes i use some predefined classes which contains some fields. And everytime i have to debug and remember that the modelbinder just don't like fields. The question: Whats the reason behind it? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
but sometimes i use some predefined classes which contains some fields While I cannot answer your question about the exact reason why the default model binder works only with properties (my guess is that it respects better encapsulation this way and avoids modifying internal state of the object which is what fields represent) I can say that what you call predefined classes should normally be view models. You should always use view models to and from your controller actions. Those view models are classes that are specifically defined to meet the requirements of the given view. So back to the main point: fields are supposed to be modified only from within the given class. They should not be accessed directly from the outside. They represent and hold internal state of the class. Properties on the other hand is what should be exposed to the outside world. Imagine that in the property getter/setter you had some custom logic. By modifying directly the field this custom logic would be broken and potentially bring the object into an inconsistent state.

Related questions

0 votes
    I have been using php-sdk to connect to facebook for a while and I never had any problems, but ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    What is the most powerful capabilities of the ModelBinder ?...
asked Nov 13, 2020 in Technology by JackTerrance
0 votes
0 votes
    MAHESH FOUND IT VERY DIFFICULT TO COPY FILES ON DOS AND REQUESTED HIS BOSS TO GET WINDOWS LOADED ON HIS SYSTEM. GIVE THE REASON WHY Select the correct answer from above options...
asked Dec 19, 2021 in Education by JackTerrance
0 votes
    Which of the below-mentioned reasons do not satisfy the reason why people create a computer virus? (a) ... -for-Cyber Security:,Cyber Security-Jobs:,Cyber Security Applications...
asked Nov 1, 2021 in Education by JackTerrance
0 votes
    Why does change in temperature doesn't effect stochastic update? (a) shape landscape depends on the network and ... function which is fixed Please answer the above question....
asked Aug 30, 2022 in Education by JackTerrance
0 votes
    The class Collector was created to generate the map with values (for example). Need that Collector contains the resource, where haves ... HashMap()); for (int i = id * 100; i...
asked Jun 2, 2022 in Education by JackTerrance
0 votes
    The class Collector was created to generate the map with values (for example). Need that Collector contains the resource, where haves ... HashMap()); for (int i = id * 100; i...
asked May 27, 2022 in Education by JackTerrance
0 votes
    I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 9, 2022 in Education by JackTerrance
0 votes
    I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 8, 2022 in Education by JackTerrance
0 votes
    I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 8, 2022 in Education by JackTerrance
0 votes
    I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 6, 2022 in Education by JackTerrance
0 votes
    I was reading the great article about binding and unbinding events (because I am a js beginner using jQuery ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    Why it doesn't display POST value? Error: Notice: Undefined index: q[abc] in line: echo $_POST[' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jan 13, 2022 in Education by JackTerrance
0 votes
    why android devolepor doesn’t use support android library for a large project? Select the correct answer from above options...
asked Dec 11, 2021 in Education by JackTerrance
...