Home .Net Core 2.0 How to instantiate and/or pass a model from Controller to Razor Page?
Reply: 0

.Net Core 2.0 How to instantiate and/or pass a model from Controller to Razor Page?

user4820
1#
user4820 Published in September 21, 2018, 8:03 am

I came to know that Razor page only works with model which is subclassed from PageModel. So I purposely create this class in the "Code-behind" of the generated razor page. I instantiate this class and pass it to the Page and the "Model" is still NULL. Controller:

    [HttpGet]
    public ActionResult Index()
    {
        long count = _service.GetData();
        IndexModel model = new IndexModel(_service.GetSortedData());
        return View("~/Pages/MyPage/Index.cshtml", model);
    }

Page:

@page
@using MyNamespace
@model MyNamespace.IndexModel
@foreach (var data in Model.Data) <= Model is NULL
<snip>

Any advice and insight is appreciated.

share|improve this question

2 Answers 2

active oldest votes
up vote -1 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.545729 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO