Home > OS >  Can't access Model items in Razor Page
Can't access Model items in Razor Page

Time:12-06

Looking to try out Dapper as a possible replacement to EFCore.

I have a simple

Model

public class CDItemFormModel
{
    public int Id { get; set; }
    public string ItemForm { get; set; }
    public bool Active { get; set; }
}

SQL DB Access

public async Task<IEnumerable<T>> LoadData<T, U>(string storedProcedure, U parameters, string connectionId = "Default")
    {
        using IDbConnection connection = new SqlConnection(_config.GetConnectionString(connectionId));

        return await connection.QueryAsync<T>(storedProcedure, parameters, commandType: CommandType.StoredProcedure);
    }

This calls an SP which does return data in SSMS

public class CDItemFormData : ICDItemFormData
{
    private readonly ISQLDataAccess _db;

    public CDItemFormData(ISQLDataAccess db)
    {
        _db = db;
    }
    // Get all Drug Forms
    public Task<IEnumerable<CDItemFormModel>> GetCDItemForms() => _db.LoadData<CDItemFormModel, dynamic>("Products.spCDItemForm_GetAll", new { });
}

While testing out I am just running against the IndexModel

public class IndexModel : PageModel
{
    private readonly ILogger<IndexModel> _logger;
    private readonly IConfiguration _configuration;
    private readonly ICDItemFormData _formData;
    
    public IEnumerable<CDItemFormModel> forms;
    
    public IndexModel(ILogger<IndexModel> logger, IConfiguration configuration, ICDItemFormData formData)
    {
        _logger = logger;
        _configuration = configuration;
        _formData = formData;
    }

    

    public async void OnGet()
    {        
        forms = await _formData.GetCDItemForms();
        
        foreach( var item in forms)
        {
            Console.WriteLine(item.ItemForm);
        }
        
        
    }
}

Simple div inside IndexModel.cshtml

<div>
    <table>
        <thead>
            <tr>
                <th>ID</th>
                <th>Form</th>
                <th>Active</th>
            </tr>
        </thead>
        <tbody>
            @foreach (var item in Model.forms)
            {
                <tr>
                    <td>
                        @item.Id
                    </td>
                    <td>
                        @item.ItemForm
                    </td>
                    <td>
                        @item.Active
                    </td>
                </tr>
            }
        </tbody>
        
    
    </table>
</div>

I added the foreach to the IndexModel to test and the 3 rows from the table are written to console. The problem is I am getting a NullReference for 'Object Reference not set to an instance of an object'

I had similar errors in EFCore when I hadn't handled the Model correctly and it was null, however, as the rows return to the console for me, I don't see how the model is null.

CodePudding user response:

try to add model to your index view page

@page 
@model IndexModel
@{
}

CodePudding user response:

It was caused by a complete rookie mistake I think.

If you check my LoadData method inside my DBAccess, it returns a Task. My OnGet was set to void but changing it to

public async Task OnGet()
{
   forms = await _formData.GetCDItemForms();
}

has fixed the issue and the cshtml foreach now displays each returned row. I have tested on a single return of GetCDItemForm(1) and it also displays correctly. I have to assume it was my Void/Task issue that caused it.

Please feel free to explain to me if it is not as simple as I have said it is.

  • Related