1

I have a .cshtml (Razor) file where i am trying to use System.Web.UI.Page.Request to access information from html input. i am developing in Visual studio 2015.

here is my code:

<!doctype html>
<html ng-app="createEvent">
@using System.Web;
@{   
    string title = Request["title"];
    string address = Request["address"];

    <p>
        You entered: <br />
        Your Title: @title <br />
        Your Address: @address
    </p>

}

With Html input:

<md-input-container class="md-block" flex-gt-xs="">
    <label>Title</label>
    <input type="text" name="title" />
</md-input-container>

<md-input-container class="md-block">
    <label>Address</label>
    <input type="text" name="address">
</md-input-container>

<md-button class="md-raised">
    <input type="submit" value="Submit" />
</md-button>

I have included System.Web in my references for the project, but it does not recognize "Request". Intellisense gives me the message: "The name 'Request' does not exist in the current context".

If i should not be trying to use "Request", how else could i get the input information into c# variables?

1
  • What type of project you've started with? Commented Mar 19, 2016 at 2:32

1 Answer 1

1

While you can access the Request object via Context.Request in a view, you should rarely do so, certainly not to access values from form fields in the same view.

You need to wrap your input elements in a form with an action URL of a controller. The controller will receive the data from the form, and you can act on it there.

@using( Html.BeginForm( "Save", "MyController", FormMethod.Post ) )
{
    // your inputs here
}
  1. Define a model that you want to populate.
  2. Create a form for the model.
  3. Submit the form to a controller action method that will automatically turn the HTTP Request into a model that can be read.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.