Displaying data from one model on the view of another model: did I do it right?

Chris76786777

This question actually has two parts to it:

  1. Is what I did correct?
  2. Is a Partial View something I should be using?

I want to display records from my Event model on the Index page of the Home controller. By default, no data is sent to the Index view of the Home controller let alone does there exist a way to display. The way I solved this was to copy most of the content of Event\Index.cshtml to Home\Index.cshtml as well as some lines from EventController.cs to HomeController.cs.

I added lines 3 and 6 to HomeController.cs as seen below:

1  public class HomeController : Controller
2  {
3    private ETODbContext db = new ETODbContext();
4    public ActionResult Index()
5   {
6      return View(db.Events.ToList());
7      //return View();
9    }
10 ...

I question whether this is the right approach because what will I do when I also want to display data from another model in the same Home\Index view?

As to my second question, since I've duplicated the view code of Event\Index.cshtml into Home\Index.cshtml I now have to maintain that code in two places. This sounds like a great time to utilize a partial view that's used in both files. Do I have the right idea here?

Vsevolod Goloviznin

As for the question about the models, you should create a ViewModel which will contain the data that you want to represent (one model in the beginning and you can add more models later):

public class HomeViewModel
{
    public IList<Event> Events {get;set;}
    public IList<AnotherModel> AnotherModels {get;set;}
}

You will then use this model in your view.

Constructing this viewmodel in controller action:

public class HomeController : Controller
{
    private ETODbContext db = new ETODbContext();
    public ActionResult Index()
    {
        var model = new HomeViewModel {Events = db.Events.ToList()};
        return View(model);
    }
}

As for the second part of the question, you can extract the similar parts of thew views into a partial view which will be then rendered in both views:

_Events.cshtml (you can place it in the Shared folder, so that it will be found automatically there)

    @model IList<Event>
    <div>
        @foreach(var ev in Model)
        {
           <h2>@ev.Name</h2>
        }
   </div>

In you home index view:

@model HomeViewModel

<div>@Html.Partial("_Events", Model.Events)</div>

In you events index view:

@model IList<Event>

<div>@Html.Partial("_Events", Model)</div>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Create record from one model in view with another view model

From Dev

MVC Moving from one Model View to Another Model View

From Dev

How to update view model data from another view model in Knockout

From Dev

Displaying data from one model and using a form to update another associated by foreign key

From Dev

How do I use a remote method in one model to return info from another model?

From Dev

Yii copying data from one model to another

From Dev

Grabbing data from database model and displaying it in a view won't show

From Dev

Pass one model from a partial view to a Parent View and bind another model to the same Parent View

From Dev

yii2 Getting array of data from one model and listing it in view of another

From Dev

Sailsjs not displaying model data associations in view

From Dev

Displaying a html view from Model Gmap

From Dev

submit form from one model to another view in Yii

From Dev

Displaying information from another model on index

From Dev

How do i show a list from a model to another View in ASP.NET MVC 4

From Dev

Save the user from one model to the another model

From Dev

How do I access data from a model the present model belongs to?

From Dev

How do I access data from a model the present model belongs to?

From Dev

Using an AngularJS directive, how do I pass a custom model from one directive to another?

From Dev

Using an AngularJS directive, how do I pass a custom model from one directive to another?

From Dev

How can I move a field from one model to another, and still retain the data?

From Dev

In AngularJS how can I data bind a model from one controller with another?

From Dev

How do I having one model listen for updates of another?

From Dev

How do I having one model listen for updates of another?

From Dev

rails fill model with data from another model

From Dev

How do I access the model data from the controller to render in the view in Ember?

From Dev

How do I access the model data from the controller to render in the view in Ember?

From Dev

How to send data from one model/ controller to another in Rails

From Dev

How to pass model class data from one page another in iOS?

From Dev

How to pass data from one model to another in Swift?

Related Related

  1. 1

    Create record from one model in view with another view model

  2. 2

    MVC Moving from one Model View to Another Model View

  3. 3

    How to update view model data from another view model in Knockout

  4. 4

    Displaying data from one model and using a form to update another associated by foreign key

  5. 5

    How do I use a remote method in one model to return info from another model?

  6. 6

    Yii copying data from one model to another

  7. 7

    Grabbing data from database model and displaying it in a view won't show

  8. 8

    Pass one model from a partial view to a Parent View and bind another model to the same Parent View

  9. 9

    yii2 Getting array of data from one model and listing it in view of another

  10. 10

    Sailsjs not displaying model data associations in view

  11. 11

    Displaying a html view from Model Gmap

  12. 12

    submit form from one model to another view in Yii

  13. 13

    Displaying information from another model on index

  14. 14

    How do i show a list from a model to another View in ASP.NET MVC 4

  15. 15

    Save the user from one model to the another model

  16. 16

    How do I access data from a model the present model belongs to?

  17. 17

    How do I access data from a model the present model belongs to?

  18. 18

    Using an AngularJS directive, how do I pass a custom model from one directive to another?

  19. 19

    Using an AngularJS directive, how do I pass a custom model from one directive to another?

  20. 20

    How can I move a field from one model to another, and still retain the data?

  21. 21

    In AngularJS how can I data bind a model from one controller with another?

  22. 22

    How do I having one model listen for updates of another?

  23. 23

    How do I having one model listen for updates of another?

  24. 24

    rails fill model with data from another model

  25. 25

    How do I access the model data from the controller to render in the view in Ember?

  26. 26

    How do I access the model data from the controller to render in the view in Ember?

  27. 27

    How to send data from one model/ controller to another in Rails

  28. 28

    How to pass model class data from one page another in iOS?

  29. 29

    How to pass data from one model to another in Swift?

HotTag

Archive