165plo
165plo

Reputation: 793

Index.cshtml(7,16): error CS0103: The name 'Request' does not exist in the current context

I am working on learning ASP .NET MVC using the dot net core framework. I have reached the point where I am trying to implement a cshtml file using some razor code. But I am getting a error when making the request.

Microsoft.AspNetCore.Mvc.Razor.Compilation.CompilationFailedException: One or more compilation failures occurred: /Views/Shared/Index.cshtml(5,4): error CS0103: The name 'IsPost' does not exist in the current context /Views/Shared/Index.cshtml(7,16): error CS0103: The name 'Request' does not exist in the current context /Views/Shared/Index.cshtml(8,16): error CS0103: The name 'Request' does not exist in the current context

My initial thought is that I am missing a reference, but I am not seeing what I would be missing. I have tried removing the razor code and then the page loads correctly.

project.json

    {
  "dependencies": {
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.FileExtensions": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Configuration.CommandLine": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Razor.Tools" : "1.0.0-preview2-final"

  },

  "tools": {
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "web.config"
    ]
  },

  "scripts": {
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  },

  "tooling": {
    "defaultNamespace": "MVCWebAPI"
  }
}

index.cshtml

<!DOCTYPE html>
@{
var totalMessage = "";
if(IsPost)
    {
    var num1 = Request["text1"];
    var num2 = Request["text2"];
    var total = num1.AsInt() + num2.AsInt();
    totalMessage = "Total = " + total;
    }
}

<html>
<body style="background-color: beige; font-family: Verdana, Arial;">
<form action="" method="post">
<p><label for="text1">First Number:</label><br>
<input type="text" name="text1"></p>
<p><label for="text2">Second Number:</label><br>
<input type="text" name="text2"></p>
<p><input type="submit" value=" Add "></p>
</form>
<p>@totalMessage</p>
</body>
</html>

Thanks

Upvotes: 2

Views: 6433

Answers (1)

Use your controller to prepare your data and pass them to your view for displaying.

For instance, Controller:

public ActionResult Index(){
    return View();
}

[HttpPost]
public ActionResult Index(ViewModelThatSuitsYourNeeds vm){
//Do your magic here
ResultViemModel resultVM= //your magic;
return View(vm);
}

But honestly, try to get a better look at MVC first, the only logic in your view should be the logic needed to display your data (eg: a foreach-loop to iterate trough a collection).

Also, take a look at @html-helpers: You don't have to create a , you can use

            @Html.LabelFor(model => model.LastName, htmlAttributes: new { @class = "control-label col-md-2" })

Of course, then I assume you pass a model to your View, which is a different story.

To get you started with MVC, you could create a simple model, use EF code-first to create the database and then add a "Controller with Views, using Entity Framework", based on that simple model and the created context, which will scaffold you a useful example.

Edit: I looked at the tutorial myself and made the “app” myself (VS 2015, 4.5.2), these are the steps I followed: -Create an Empty asp.net web-application, no references/folders or Authentication. -Create the folder Controllers and add a HomeController (empty one) -Wait till the scaffolding is done -Add a view (Index.cshtml) to the Views-folder, without a model, but with the layout-page -Copy the code from W3S, but strip the markup that already is included in the layout-page (like Body):

    @{
        ViewBag.Title = "Index";
    }

    <h2>Index</h2>

    @{
        var totalMessage = "";
        if (IsPost)
        {
            var num1 = Request["text1"];
            var num2 = Request["text2"];
            var total = num1.AsInt() + num2.AsInt();
            totalMessage = "Total = " + total;
        }
    }

        <form action="" method="post">
            <p>
                <label for="text1">First Number:</label><br>
                <input type="text" name="text1">
            </p>
            <p>
                <label for="text2">Second Number:</label><br>
                <input type="text" name="text2">
            </p>
            <p><input type="submit" value=" Add "></p>
        </form>
        <p>@totalMessage</p>

-Run it, works fine here

If it doesn’t work on your machine, maybe consider uploading the project, makes it easier to check!

Upvotes: 3

Related Questions