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

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

Kok How Teh
1#
Kok How Teh Published in 2018-02-12 02:31:20Z

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.

Kok How Teh
2#
Kok How Teh Reply to 2018-02-12 03:13:11Z

The good old "Controller" is gone. It's all in the "Code-behind" of the page.

Saineshwar
3#
Saineshwar Reply to 2018-02-12 09:55:43Z

Below is Example which will help you to understand what are razor pages.

What are Razor pages?

Razor Pages is a new feature of ASP.NET Core MVC that makes coding page-focused scenarios easier and more productive. (definition from - https://docs.microsoft.com/en-us/aspnet/core/mvc/razor-pages/?tabs=visual-studio)

Where to use Razor pages?

There are some pages in the application which are not too big where you still require creating a controller and add action Method, along with that we need to add View.

In this part we can use Razor Pages, which has code behind in it, we just need to add a Razor Page and on view “Customer.cshtml” you can design your view and on the same page you can write code for handling requests such as Get and Post. However, if you think you want to separate it then you can use code behind of that “Customer.cshtml.cs”

Code Snippet while assigning values to Model

using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using RazorPagesDemo.Models;
using System.Collections.Generic;
using System.Linq;
namespace RazorPagesDemo.Pages
{
    public class AllCustomerModel : PageModel
    {
        DatabaseContext _Context;
        public AllCustomerModel(DatabaseContext databasecontext)
        {
            _Context = databasecontext;
        }

        public List<Customer> CustomerList { get; set; }
        public void OnGet()
        {
          // Assigning value to Model
            var data = (from customerlist in _Context.CustomerTB
                        select customerlist).ToList();

            CustomerList = data;
        }
    }
}

View where Model is used for Display data.

@page
@using RazorPagesDemo.Models
@model AllCustomerModel

<h2>Index</h2>

<p>
    <a asp-page="Customer">Create New Customer</a>
</p>
<table class="table">
    <thead>
        <tr>
            <th>
                @Html.DisplayName("Name")
            </th>
            <th>
                @Html.DisplayName("Address")
            </th>
            <th>
                @Html.DisplayName("Country")
            </th>
            <th>
                @Html.DisplayName("City")
            </th>
            <th>
                @Html.DisplayName("Phoneno")
            </th>
            <th>Edit | Delete</th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model.CustomerList)
        {
            <tr>
                <td>
                    @Html.DisplayFor(modelItem => item.Name)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Address)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Country)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.City)
                </td>
                <td>
                    @Html.DisplayFor(modelItem => item.Phoneno)
                </td>
                <td>
                    <a asp-page="./EditCustomer" asp-route-id="@item.CustomerID">Edit</a> |
                    <a asp-page="./AllCustomer" onclick="return confirm('Are you sure you want to delete this item?');" asp-page-handler="Delete" asp-route-id="@item.CustomerID">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

link of Detail Article :- Link for Complete Article

You need to login account before you can post.

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

© 2016 Powered by mzan.com design MATCHINFO