I've created Model using "ADO.NET Entity Data Model" which has property for 2 Tables.
Now i'm facing challenge to connect this to controller type "MVC5 Controllor with View using Entity Framework" ?
I could do this with model having one Table property only, However how can i make it for Model with More than one table.
I suspect to create new .cs file which can encompass the both table property, How ?
once new .CS file created how i can map that to the Controller type"MVC5 Controllor with View using Entity Framework" ?
A model is a class. The tables are properties of the model class. The programming pattern can look similar to the following.
public class MultipleTablesModel
{
public List<Table1> Table1Records { get; set; }
public List<Table2> Table2Records { get; set; }
}
Usually there is a relationship between tables which is unknown since you have no provided any code. If you are still having trouble, share enough code so the community can understand how your application works.
If you need to display the data of multiple models at the same time in a view, then as @mgebhard said, you can
create a new class(new model) to put the two tablesin your project .
You don't need to take out all the fields of these two tables, you just need to call the table's class, and then return the new class to the view in the controller.
Specific content you can refer to the following cases:
Two existing tables' classes:
public class Classroom
{
public int id { get; set; }
public string class_name { get; set; }
public string student_name { get; set; }
public DateTime join_date { get; set; }
}
public class School
{
public int id { get; set; }
public string school_name { get; set; }
}
Create a new class (new model) in Models folder:
public class MultipleTablesModel
{
public List<Classroom> Table1Records { get; set; }
public List<School> Table2Records { get; set; }
}
Controller:
public ActionResult Index()
{
var context = new MyContext();
MultipleTablesModel multipleTable = new MultipleTablesModel()
{
Table1Records = context.Classroom.ToList(),
Table2Records = context.School.ToList(),
};
return View(multipleTable);
}
Thanks you So much for your response, I'd really your solution, However i just wanted to do this same operation by using Controller Type "MVC5 Controller with Views, using Entity Framework"
Thanks you So much for your response, I'd really your solution, However i just wanted to do this same operation by using Controller Type "MVC5 Controller with Views, using Entity Framework"
Your response would be appreciated !!!
And that's what we showed you with the code examples. We cannot see your project and you did not share source code. The best I can recommend is going through the standard getting started tutorials to learn the basics.
Do you mean you are generating code? This kind of tool can't handle everything and should be considered as a starting point. More likely here you have no other option than to edit the generated code so that it can do what you want. As pointed already the
model is just a C# class that could be as complex as needed.
Thanks you so much for your response. Your Suggested Link and Code has much more useful to me to work around and get my things done, However i was looking for Auto Scaffolding Code generation by the Controller with view, Entity Framework.
Kindly go through the below link, My expectation of output is similar to kind of show in that.
In above article , Artist.cs ,Song.cs and later their DbContext class created Manually , I've done same operations with option of Adding
RightClick on MODEL-->>ADD-->>ADO.NETEntityDataModel.
There i've selected both the Artist&Songs Tables, Hence Program has created the 3 .CS Files , First Artist.cs, Second Song.cs and Third Model with has combined attributed of both Tables with DbContext
Later on i've followed same steps given in the article (From Line "Displaying Data From a Single Table") to add the Controller , This would create the AutoScaffold Action Method.
Now i should able to access all the Field from both of the Tables in DETAILS Action Method.
@foreach (var item in Model.Artist)
@foreach (var item in Model.Songs)
However this only referring table Columns Artist , I
couldn't able to refer the Songs Table.
Mainly the Code you suggested is helpful to
perform expected operation, However you explain this with Empty Controller , So how i can make it for the "Controller with View , Entity Framework".
In general, we do not recommend that you use the "Controller with View, Entity Framework" pattern to create views under the premise of this requirement.
Because there is no substantial connection between the several classes you provide, if you must create views through the "Controller with View, Entity Framework" pattern, then you need to create foreign keys for these classes to associate and then reference
one of these classes creates a view and displays data from multiple tables through connected relationships.
If the two classes are not really related, I don't recommend this approach.I still recommend that you create an "Empty Controller" and implement it in accordance with the method in my previous reply.
Best Regards,
YongQing.
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Thank you so much for your response, I went with your suggestion and now it's going well.
However i encountered with one more below problem.
I've a requirement where i need to display , the data from Table1 and Table2 (i can achieved this with above logic) and later on the same page need to add new data entry in the Table2 .
This is a common requirement of adding data in mvc.
At this time, you need to ignore the multipletables model, and then you can use add button creates its click method in js and calls ajax to pass your new data to the controller.
At the same time, you can use the modal pop-up box of bootstrap or a new view to fill your new data.
There are many similar cases on the Internet. You can refer to the following links:
None
0 Points
4 Posts
Add a Multiple View in single Model using ADO.NET Entity Data Model
Feb 06, 2020 11:11 AM|jaiganeshkalbande|LINK
I need a help on how on below situation wherein
I've created Model using "ADO.NET Entity Data Model" which has property for 2 Tables.
Now i'm facing challenge to connect this to controller type "MVC5 Controllor with View using Entity Framework" ?
I could do this with model having one Table property only, However how can i make it for Model with More than one table.
I suspect to create new .cs file which can encompass the both table property, How ?
once new .CS file created how i can map that to the Controller type"MVC5 Controllor with View using Entity Framework" ?
All-Star
52971 Points
23574 Posts
Re: Add a Multiple View in single Model using ADO.NET Entity Data Model
Feb 06, 2020 01:03 PM|mgebhard|LINK
A model is a class. The tables are properties of the model class. The programming pattern can look similar to the following.
Usually there is a relationship between tables which is unknown since you have no provided any code. If you are still having trouble, share enough code so the community can understand how your application works.
Contributor
3710 Points
1043 Posts
Re: Add a Multiple View in single Model using ADO.NET Entity Data Model
Feb 07, 2020 07:06 AM|Yongqing Yu|LINK
Hi jaiganeshkalbande ,
If you need to display the data of multiple models at the same time in a view, then as @mgebhard said, you can create a new class(new model) to put the two tables in your project .
You don't need to take out all the fields of these two tables, you just need to call the table's class, and then return the new class to the view in the controller.
Specific content you can refer to the following cases:
Two existing tables' classes:
Create a new class (new model) in Models folder:
Controller:
View:
Here is the result of this demo:
You can also refer to this link:
Multiple Models In Single View Using Entity Framework
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
None
0 Points
4 Posts
Re: Add a Multiple View in single Model using ADO.NET Entity Data Model
Feb 07, 2020 01:45 PM|jaiganeshkalbande|LINK
Hello ,
Thanks you So much for your response, I'd really your solution, However i just wanted to do this same operation by using Controller Type "MVC5 Controller with Views, using Entity Framework"
Your response would be appreciated !!!
All-Star
52971 Points
23574 Posts
Re: Add a Multiple View in single Model using ADO.NET Entity Data Model
Feb 07, 2020 02:27 PM|mgebhard|LINK
And that's what we showed you with the code examples. We cannot see your project and you did not share source code. The best I can recommend is going through the standard getting started tutorials to learn the basics.
https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
All-Star
48490 Points
18071 Posts
Re: Add a Multiple View in single Model using ADO.NET Entity Data Model
Feb 07, 2020 02:36 PM|PatriceSc|LINK
Hi,
Do you mean you are generating code? This kind of tool can't handle everything and should be considered as a starting point. More likely here you have no other option than to edit the generated code so that it can do what you want. As pointed already the model is just a C# class that could be as complex as needed.
None
0 Points
4 Posts
Re: Add a Multiple View in single Model using ADO.NET Entity Data Model
Feb 08, 2020 12:18 PM|jaiganeshkalbande|LINK
Thanks you so much for your response. Your Suggested Link and Code has much more useful to me to work around and get my things done, However i was looking for Auto Scaffolding Code generation by the Controller with view, Entity Framework.
Kindly go through the below link, My expectation of output is similar to kind of show in that.
https://sensibledev.com/display-data-from-multiple-tables-in-a-single-mvc-view/
In above article , Artist.cs ,Song.cs and later their DbContext class created Manually , I've done same operations with option of Adding
RightClick on MODEL-->>ADD-->>ADO.NETEntityDataModel.
There i've selected both the Artist&Songs Tables, Hence Program has created the 3 .CS Files , First Artist.cs, Second Song.cs and Third Model with has combined attributed of both Tables with DbContext
Later on i've followed same steps given in the article (From Line "Displaying Data From a Single Table") to add the Controller , This would create the AutoScaffold Action Method.
Now i should able to access all the Field from both of the Tables in DETAILS Action Method.
@foreach (var item in Model.Artist)
@foreach (var item in Model.Songs)
However this only referring table Columns Artist , I couldn't able to refer the Songs Table.
Mainly the Code you suggested is helpful to perform expected operation, However you explain this with Empty Controller , So how i can make it for the "Controller with View , Entity Framework".
You if please can suggest on this
Contributor
3710 Points
1043 Posts
Re: Add a Multiple View in single Model using ADO.NET Entity Data Model
Feb 10, 2020 09:41 AM|Yongqing Yu|LINK
Hi jaiganeshkalbande ,
In general, we do not recommend that you use the "Controller with View, Entity Framework" pattern to create views under the premise of this requirement.
Because there is no substantial connection between the several classes you provide, if you must create views through the "Controller with View, Entity Framework" pattern, then you need to create foreign keys for these classes to associate and then reference one of these classes creates a view and displays data from multiple tables through connected relationships.
Razor views, displaying a property linked by a foreign key
If the two classes are not really related, I don't recommend this approach. I still recommend that you create an "Empty Controller" and implement it in accordance with the method in my previous reply.
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
None
0 Points
4 Posts
Re: Add a Multiple View in single Model using ADO.NET Entity Data Model
Feb 14, 2020 04:16 PM|jaiganeshkalbande|LINK
Thank you so much for your response, I went with your suggestion and now it's going well.
However i encountered with one more below problem.
I've a requirement where i need to display , the data from Table1 and Table2 (i can achieved this with above logic) and later on the same page need to add new data entry in the Table2 .
Request you to please suggest something on this.
Contributor
3710 Points
1043 Posts
Re: Add a Multiple View in single Model using ADO.NET Entity Data Model
Feb 19, 2020 10:24 AM|Yongqing Yu|LINK
Hi jaiganeshkalbande,
This is a common requirement of adding data in mvc.
At this time, you need to ignore the multipletables model, and then you can use add button creates its click method in js and calls ajax to pass your new data to the controller.
At the same time, you can use the modal pop-up box of bootstrap or a new view to fill your new data.
There are many similar cases on the Internet. You can refer to the following links:
CRUD Application In ASP.NET MVC With Entity Framework
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.