Deserialize with LINQ query code in PageModel or PageView Today I do something like this, where I get, select and sort some data and pass it on to the view (which loads a partial view) as a list of custom objects

Page model

public PagesSettings PageSettings { get; set; }
public List&lt;Reportage&gt; itemListReportages { get; set; }

public void OnGet()
{
    PageSettings = (PagesSettings)HttpContext.Items["pagesettings"];

    if (PageSettings == null)
    {
        return;
    }

    var fileContent = _fileService.GetFileContentCached(PageSettings.DataFile);

    if (fileContent?.Length &gt; 0)
    {
        itemListReportages = (JsonConvert.DeserializeObject&lt;List&lt;Reportage&gt;&gt;(fileContent))
            .Where(l =&gt; l.Published)
            .OrderBy(l =&gt; l.Name)
            .ToList();
    }
}


Page view

@page
@model ListReportagesModel

@if (Model.PageSettings?.Data?.Count &gt; 1 &amp;&amp; Model.PageSettings.Data[1].Html?.Length &gt; 0)
{
    &lt;section class="content @(Model.PageSettings.SectionClass ?? "")"&gt;
        &lt;div&gt;
            @if (Model.PageSettings.Data[1].Values?.Length &gt; 0)
                {
                    @Html.Raw(String.Format(Model.PageSettings.Data[1].Html, Model.PageSettings.Data[1].Values))
                }
                else
                {
                    @Html.Raw(@Model.PageSettings.Data[1].Html)
                }
        &lt;/div&gt;
    &lt;/section&gt;
}

&lt;section class="content @(Model.PageSettings.SectionClass ?? "")"&gt;
    &lt;div class="aflex aflexwrap aflexspacebetween"&gt;
        &lt;partial name="/Pages/Partial/@Model.PageSettings.PartialView" /&gt;
    &lt;/div&gt;
&lt;/section&gt;


Partial view

@for (var i = 0; i &lt; Model.itemListReportages.Count; i++)
{
    &lt;div class="item"&gt;
        &lt;div class="itembkg" style="background-image: url('@Model.itemListReportages[i].BkgImage');"&gt;&lt;/div&gt;
        &lt;div class="itemtxt"&gt;
            &lt;span&gt;@Model.itemListReportages[i].Text&lt;/span&gt;
        &lt;/div&gt;
    &lt;/div&gt;
}




It now start to become a lot of PageModel's, so I started to wonder where the proper location to run C# code is. In above the data loading/select/sort is done in the PageModel and the rest in the view's.

I was planning to split it up like below instead, moving the select/sort from Pagemodel to the partial view, and with that reduce the amount of Pagemodel's and make it simpler to add data by simply create a new class + data file + partial view.

The question, which way is recommended (if any), perform select/sort in PageModel or in View?

Page model

public PagesSettings PageSettings { get; set; }
public string fileContent { get; set; }

public void OnGet()
{
    PageSettings = (PagesSettings)HttpContext.Items["pagesettings"];

    if (PageSettings == null)
    {
        return;
    }

    fileContent = _fileService.GetFileContentCached(PageSettings.DataFile);
}


Partial view

if (fileContent?.Length &gt; 0)
{
    var itemListReportages = (JsonConvert.DeserializeObject&lt;List&lt;Reportage&gt;&gt;(fileContent))
        .Where(l =&gt; l.Published)
        .OrderBy(l =&gt; l.Name)
        .ToList();

    @for (var i = 0; i &lt; Model.itemListReportages.Count; i++)
    {
        &lt;div class="item"&gt;
            &lt;div class="itembkg" style="background-image: url('@Model.itemListReportages[i].BkgImage');"&gt;&lt;/div&gt;
            &lt;div class="itemtxt"&gt;
                &lt;span&gt;@Model.itemListReportages[i].Text&lt;/span&gt;
            &lt;/div&gt;
        &lt;/div&gt;
    }
}
I find you should keep the library code in the model and only parts that are relevant to views inside them.



In your case there is actually a third option. Let OnGet only handle the file. Remove OrderBy as this is a view's matter how it's displayed. Additionally initialize itemListReportages to an empty list if a file was empty. It'll save you the trouble of null checks later.

public void OnGet()
{
    PageSettings = (PagesSettings)HttpContext.Items["pagesettings"];

    if (PageSettings == null)
    {
        return;
    }

    var fileContent = _fileService.GetFileContentCached(PageSettings.DataFile);

    itemListReportages = 
        fileContent?.Length &gt; 0
            ? JsonConvert.DeserializeObject&lt;List&lt;Reportage&gt;&gt;(fileContent).Where(l =&gt; l.Published).ToList()
            : new List&lt;Reportage&gt;();
}


We don't see whether you initialize itemListReportages but this view could crash because you don't use the null propagation operator here: ?.Count


@for (var i = 0; i &lt; Model.itemListReportages.Count; i++)
{
    &lt;div class="item"&gt;
        &lt;div class="itembkg" style="background-image: url('@Model.itemListReportages[i].BkgImage');"&gt;&lt;/div&gt;
        &lt;div class="itemtxt"&gt;
            &lt;span&gt;@Model.itemListReportages[i].Text&lt;/span&gt;
        &lt;/div&gt;
    &lt;/div&gt;
}



With a non-null list you can turn this into a foreach loop and put OrderBy here. It'll make the code shorter too as you now have an item variable and no longer have to acces them by index like Model.itemListReportages[i]:

@foreach (var item in Model.itemListReportages.OrderBy(x =&gt; x.Name))
{
    &lt;div class="item"&gt;
        &lt;div class="itembkg" style="background-image: url('@item.BkgImage');"&gt;&lt;/div&gt;
        &lt;div class="itemtxt"&gt;
            &lt;span&gt;@item.Text&lt;/span&gt;
        &lt;/div&gt;
    &lt;/div&gt;
}