locked
Razor Doesn't seem to grab the instance RRS feed

  • Question

  • User-974902291 posted

    Hi, all, with the following code, I get errors like this: "NullReferenceException: Object reference not set to an instance of an object", due to line 73 of my cshtml file.

    I assume it's not able to grab a reference to an instance of the model, IndexModel, but I've been googling all night and trying various things, and still cannot figure out why.

    Just trying to build a working example for myself so I can move forward...

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.ComponentModel.DataAnnotations;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.RazorPages;
    using aspNet_Sep_3_b.Models;
    
    namespace aspNet_Sep_3_b.Models
    {
        public class IndexModel : PageModel
        {
    
            public readonly aspNet_Sep_3_b.Models.MathsCalc _context;
    
            public IndexModel(aspNet_Sep_3_b.Models.MathsCalc context)
            {
                _context = context;
            }
    
            public IActionResult OnGet()
            {
                return Page();
            }
    
            public async Task<IActionResult> OnPostAsync()
            {
    
                //Console.Beep();
    
    
                if (!ModelState.IsValid)
                {
                    return Page();
                }
    
                int result = _context.FirstNumber + _context.SecondNumber;
    
                _context.Result = result;
    
                //Console.WriteLine(result);
    
                await _context.SaveChangesAsync();
    
                return RedirectToPage();
            }
        }
    }
    @*@page*@
    @model aspNet_Sep_3_b.Models.IndexModel
    
    
    <h2>Simple Calculation In Razor Pages</h2>
    
    <form method="post">
        <div asp-validation-summary="ModelOnly"></div>
        <div class="form-group">
            <div>
                <label asp-for="@Model._context.FirstNumber">First</label>
                <input asp-for="@Model._context.FirstNumber" />
                <span asp-validation-for="@Model._context.FirstNumber"></span>
            </div>
        </div>
        <div class="form-group">
            <div>
                <label asp-for="@Model._context.SecondNumber">Second</label>
                <input asp-for="@Model._context.SecondNumber" />
                <span asp-validation-for="@Model._context.SecondNumber"></span>
            </div>
        </div>
        <div class="form-group">
            <input type="submit" value="Create" class="btn btn-default" /> <!--Show Result</input>-->
        </div>
        <div class="form-group"><label>Result: @(Model._context.Result)</label></div>
    </form>
    using System;
    using System.ComponentModel.DataAnnotations;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.EntityFrameworkCore;
    
    namespace aspNet_Sep_3_b.Models
    {
        public class MathsCalc : DbContext
        {
            [Required]
            [BindProperty]
            public int FirstNumber { get; set; } = 0;
    
            [Required]
            [BindProperty]
            public int SecondNumber { get; set; } = 0;
    
            [TempData]
            public int Result { get; set; } = 0;
    
        }
    }

    Wednesday, October 9, 2019 5:58 PM

All replies

  • User475983607 posted

    glestrade

    Just trying to build a working example for myself so I can move forward...

    You're not following standard patterns and practices.   I recommend bookmarking the following site.  The link covers model binding.

    https://www.learnrazorpages.com/razor-pages/model-binding

    Add Entity Framework once you understand how model binding works.

    https://docs.microsoft.com/en-us/aspnet/core/data/ef-rp/intro?view=aspnetcore-3.0&tabs=visual-studio

    Wednesday, October 9, 2019 6:11 PM
  • User-974902291 posted

    I'm going to reveal my lack of knowledge here, but should I be concerned if these tutorials are in core 3.0, while I'm still on ~2.2?

    Wednesday, October 9, 2019 6:17 PM
  • User475983607 posted

    I'm going to reveal my lack of knowledge here, but should I be concerned if these tutorials are in core 3.0, while I'm still on ~2.2?

    The concepts behind Model Binding and Entity Framework are the same .  But if you are just starting out than why not start with the latest version which is Core 3.0?

    Wednesday, October 9, 2019 6:23 PM
  • User-974902291 posted

    I am assuming that companies may favor people who know the old stack. They likely have old code that needs to be maintained. This project is essentially for resume building.

    Wednesday, October 9, 2019 6:35 PM
  • User475983607 posted

    I am assuming that companies may favor people who know the old stack. They likely have old code that needs to be maintained. This project is essentially for resume building.

    You might want to visit the Core support schedule/policy.  I'm pretty sure most companies implementing Core will upgrade with LTS schedule.

    https://dotnet.microsoft.com/platform/support/policy/dotnet-core

    IMHO, the old stack is ASP.NET; Web Forms and MVC.

    Wednesday, October 9, 2019 6:47 PM
  • User-821857111 posted

    I am assuming that companies may favor people who know the old stack. They likely have old code that needs to be maintained. This project is essentially for resume building.

    The practical difference between ASP.NET Core 2.2 and 3.0 is negligible - mostly implementation details. There are some limited code changes in the Startup file to accommodate those implementation changes, but other than that, nothing remarkable at all.

    https://www.mikesdotnetting.com/article/337/whats-new-in-net-core-3-0-for-razor-pages

    Thursday, October 10, 2019 6:07 AM