0

This might be a respeat of the question posted here but I did not find any working answer.

Say I have project1 which uses the model model1. I create a new project, project2, in the same solution and I want to use the model1 in a view of project2. is this possible and how do I call this model in my view and controller of project2?

2
  • Do you want the model and controller between both projects to always be exactly the same? Commented Dec 21, 2012 at 17:37
  • Yes please. I just want to have to set the model only in one project and make it available in the other Commented Dec 21, 2012 at 17:40

2 Answers 2

3

Why could you not move your models into a third class assembly that is referenced by both of your client projects? This way your model truly is defined once and can be used by both projects.

Doing it the way that Erik recommends would result in duplicate source files compiled into each project independently rather than truly sharing the same codebase based on a referenced assembly.

Sign up to request clarification or add additional context in comments.

8 Comments

Can you please explain what you mean by "third class assembly"? Is this class supposed to be created in either one of the projects?
Within your solution, you would have 3 projects defined. 2 would be your end websites, and then a third project would be just a "class library" that would contain plain POCO objects to be used as your models. Both of your web projects would then reference this class library and use the models from that namespace.
So after creating the project of type "Class Library", do I do ahead a create new classes in it that will serve as models? and how do I reference these models in my projects 1 and 2?
Yes, your models will exist within the class library project, and NOT within the web project. Then from each of your web projects, you would add a project reference back to the models assembly. Details on this can be found on MSDN: msdn.microsoft.com/en-us/library/7314433t(v=vs.90).aspx
Thank you. One more ques. is it possible to make a database connection in the library?
|
1

I'm not entirely sure on the purpose of the model that you want to share (it would be nice if you gave us a bit more details on how it will be used), but to share just the model you can create a new project called SharedModels in the same solution where the your projects 1 and 2 are and then reference SharedModels project in both of them.

7 Comments

But how do i do the referencing?
In the project where you want to have the reference Right click on the References -> Add Reference... -> select Project under Solution on the left -> select the common project
Say my model name in the new porject is model1. After doing what you suggested, how do I reference that in my view?
Just like you specify a model for a view in any situation... @model [namespace].[model name]. Here is the thing though. What you are asking is highly uncommon and I never had to do anything like that. The chances are, whatever you are doing, you are doing it wrong. I suggest you start with the description of what you are actually trying to accomplish.
I have several project which use the same model and I do not want to recreate the model each time, but simply link to a main model. The models also need to connect to a database
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.