JESTech
JESTech

Reputation: 145

How to use MVC to save form's (view) data that belongs to two different tables?

I'm an experienced Java Developer working with a web-application project built using MVC/C#/Razor. I need to modify the project so that it retrieves data from two separate tables; there are 2 columns that no longer belong to table1 (and its pertaining Model) but now need to be saved to the new table2 once the form is submitted.

I still don't quite get the MVC separation. Does Models are exact table representations, form representations or both. And if they can be both how do I "distribute" a form's data across the tables that are in charge of saving it.

Sorry if I'm not clear but I'm very new to Razor, C#, ASP.NET and MS's MVC.

Thanks for any help!

Upvotes: 1

Views: 909

Answers (1)

Soner Sevinc
Soner Sevinc

Reputation: 400

İf i did not get you wrong , you want to combine tables in 1 model and return 1 model.

You can create a custom model representing the data needed for your view.

public class UserView
{
public User User{get;set;}
public List<Messages> Messages{get;set;}
}

And then,

return View(new UserView(){ User = user, Messages = message});

In the view:

Model.User;
Model.Messages;

Hope shows you a way to solve your problem.

Upvotes: 1

Related Questions