skip to Main Content

I’m trying to follow the Microsoft Docs on getting started with Razor. When I try to implement the following code I get the errors detailed in the comments:

Solution/Project/Pages/AddNumbers.cshtml :

@{
    var total = 0;
    var totalMessage = "";
    if(IsPost) { // The name 'isPost' does not exist in the current context

        // Retrieve the numbers that the user entered.
        var num1 = Request["text1"]; // CS0103  The name 'Request' does not exist in the current context
        var num2 = Request["text2"]; // CS0103  The name 'Request' does not exist in the current context

        // Convert the entered strings into integers numbers and add.
        total = num1.AsInt() + num2.AsInt();
        totalMessage = "Total = " + total;
    }
}

I think I’ve followed the instructions faithfully, but can’t think where I’ve made an error. What’s the fix?

2

Answers


  1. Chosen as BEST ANSWER

    I think the problem is that the documentation I was following is for "ASP.NET Web Pages (Razor) 3", but I was trying to run the code on "ASP.NET Core Web App".

    As Lajos Arpad pointed out, I would need to create my own isPost property .


  2. IsPost does not exist if you do not create it. You can create such a property, defaulting to false and setting it to true in your OnPost handler.

    Similarly, you cannot refer to Request like you tried in your code, you need a property for that too.

    Since num1 and num2 were not successfully initialized, they cannot be successfully converted to int either.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search