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

I am new to MVC and learning things. Kindly help me that I am stuck in where I want to create a strongly typed view from two strongly typed model .

Say for an example I have EmployerAccount and EmployerCompany models.

And I have a create view, which should get user input for employerAccount and Employer Company.

Kindly help

regards
Posted

1 solution

You can not develop the View that can use more then one model.
but you can fix this issue by either developing dummy model which has property from both existing mode you have,
or you can develop new model which has two members i.e. existing model class
or you can use the inheritance concept.
 
Share this answer
 
Comments
Ahamed Azeem 23-Oct-12 4:35am    
Thanks. I already tried creating a dummy model, which instantiates the two models. for ex:
public class EmployerModel
{
public Models.EmployerAccount EmployerAccount;

public Models.EmployerCompany EmployerCompany;


}

and I used a view to use this dummy model. everything worked fine and it created a textbox for the second model as well.

But while saving to the database, it throws error cannot pass the null values

so could you please explain me with an example
Zoltán Zörgő 23-Oct-12 4:52am    
You are on the right path!
What is the code of your action that should handle the submitted data?
Ahamed Azeem 23-Oct-12 4:55am    
yup I feel the problem is there, becoz the controller action methods gets the EmployerAccount class object and I dont know passing the dummy class object is correct or not. though I am passing it, it says the error in db.savechanges method


public ActionResult Create(EmployerAccount ea)
{
db.EmployerAccounts.AddObject(ea);

db.SaveChanges();

return View();
}
Zoltán Zörgő 23-Oct-12 5:01am    
And what's the view code? The problem is, that you can use view models, but than it's up to you to handle the data model properly. Your view model is made of two data models. So you will get that type back also.
Zoltán Zörgő 23-Oct-12 5:08am    
You could find this interesting: http://odetocode.com/blogs/scott/archive/2012/03/11/complete-guide-to-mass-assignment-in-asp-net-mvc.aspx

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