locked
refrence two models in same razor page RRS feed

  • Question

  • User1655654435 posted

    At the top of my .cshtml page I have a @model IEnumerable<TestApp.Models.Mote> of a model type that i iterate over a list so i can display the data there:

     @foreach (TestApp.Models.Mote r in Model)
                    {
                        <p>
                            @r.Motenummer
                            @r.Date.Value.ToString("d")
                            @r.Sted
                            @r.Time.Value.ToString("t")                        
                        </p>
                    }

    I also have a form on the page that posts to a different model. But when I put a reference to that model in my razor page It blanks out the first one. 

    Any ideas on how to do this?

    Wednesday, March 22, 2017 6:25 PM

Answers

  • User475983607 posted

    Create a class the has encapsulates both items.

        public class MyViewModel
        {
            public IEnumerable<TestApp.Models.Mote> Mote { get; set; }
            public MyOtherModel otherModel { get; set; }
        }

    Populate this new ViewModel class in the controller and pass the ViewModel to the View.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, March 22, 2017 6:30 PM