0

net I am trying to take a value from a previous form typically in php it would be written like this

$name= $_POST ["Username"];
$pass= $_POST ["Password"]; 

how can I write this in asp.net

Aya Abdelsalam
  • 502
  • 3
  • 8
  • 21
  • What are you using asp.net webforms page? mvc controller ? http handler ? – adt Jun 23 '12 at 07:28
  • that's asp.net mvc, so it's been asked and answered before. http://stackoverflow.com/questions/5088450/simple-mvc3-question-how-to-retreive-form-values-from-httppost-dictionary-or – Ray Cheng Jun 23 '12 at 07:36

3 Answers3

1

if you use GET

string usrnm = Request.QueryString["username"];
string pass = Request.QueryString["password"];

if you use POST

 string usrnm = Request.Form["username"];
 string pass = Request.Form["password"];
KAPLANDROID
  • 1,099
  • 1
  • 12
  • 23
  • Thanks for ur reply I did that but it isnt working maybe there is something wrong with my query is this "SELECT first_name FROM Student WHERE username=@usrnm And password=@pass" Is this correct – Aya Abdelsalam Jun 23 '12 at 07:41
0

in web forms

if (Page.IsPostBack)
{
  //access posted input as
    Request.Form["input1"]
    Request.Form["input2"]
    Request.Form["input3"]
}

in mvc

[HttpPost]
 public ActionResult myaction(strig input1,strig input1,strig input1)
        { 
            //you can access your input here
            return View();
        }

or if you have view model for it, which is capable to take 3 inputs as

public class myViewmodleclass()
{
    public string input1{get;set;}
    public string input2{get;set;}
    public string input3{get;set;}
}

controller action

[HttpPost]
public ActionResult myaction(myViewmodleclass mymodelobject)
{ 
  //you can access your input here
  return View();
}
Raab
  • 34,778
  • 4
  • 50
  • 65
  • 1
    I guessed from your question, sorry if I misjudged. you must watch these vdos to learn mvc http://www.pluralsight-training.net/microsoft/Courses/TableOfContents?courseName=aspdotnet-mvc3-intro – Raab Jun 23 '12 at 08:08
0

so you using mvc, which has a nice model binding.

So you are using mvc which has nice model binding. You can simply have a proper model object. For your example

public class LoginInfo()
{
    public string UserName{get;set;}
    public string Password {get;set;}
}

in your controller

[HttpPost] 
public ActionResult Logon(LoginInfo loginInfo )
{
   // Do stuff with loginInfo
}
adt
  • 4,320
  • 5
  • 35
  • 54
  • Thank you sooo much for your reply I am still trying to understand mvc so may I ask in which file format should I save my controller and model and which of them is responsible for retrieving the values entered in the view – Aya Abdelsalam Jun 23 '12 at 07:44