Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys

I'm sorry about this, but I'm struggling to find info on this.

I'm writing my first MVC app with MVC4 and Razor in VS2010. I was using my controller to take a parameter from the URL and execute logic and data from this all in said controller. Now I've decided to try and do things properly and have added the logic and fields into a Model.

The model has a constructor which accepts a parameter to do the same stuff I was doing in the controller.

My question is this; how do I get this parameter into the model so the model can get the info it needs?

Thanks
Posted

1 solution

Quote:
Now I've decided to try and do things properly and have added the logic and fields into a Model.
Wrong! The model is the model, your logic belongs in a separate business logic layer or in the controller - but not in the model.
The controller should pass the incoming, validated data to the layers below.
 
Share this answer
 
v2
Comments
Unsy 13-Oct-12 16:23pm    
It kind of is... I was trying to simplify it. I have a model where in the get and set parts of the properties, I make calls to an external DLL file that I have built which communicates with the database etc rather than using Entity Framework. The info from this DLL is then reflected in the model with the annotations. I suppose I am wrong in having the model take the parameter with a constructor, but how else can I populate the model? Or am I completely misusing the model?
Zoltán Zörgő 13-Oct-12 16:34pm    
Kind of..
If you follow the repository pattern, you will not mix the repository functions with the model. The model should only contain the minimum of logic: some calculated read-only properties - aside the other properties of course. Constructors can be used, but might mislead you. Try to keep the model as clean as you can. But the nobody is telling you how to populate the entity collection in the repository: it is up to you to return an array, an EF collection or whatever you want. This way you get a bonus: you can change your mind, and alter the way you populate your repository collection without affecting other parts of your code. And if you use IoC, it will be even simpler.
Unsy 13-Oct-12 16:48pm    
Gotcha. Thanks. I'll take another look at what I'm doing... I'll clean up my model, and see what I can do.

Thank you. :)
Zoltán Zörgő 13-Oct-12 16:52pm    
Hope I helped you. If you find my answer of use, feel free to accept it :)
Unsy 13-Oct-12 16:54pm    
You have been a star. I may be back with more questions at some point, but what you have given me should do for this post.

Thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900