Joe G Joe G - 2 months ago 12
HTML Question

CSHTML displaying user inputs

My web assignment has asked me to create a dating site login page and display the information, i have completed the HTML to a point and am now beginning with the C#. I have began this but have come to a standstill as I dont know what is going wrong to display the information.

My question is: How do I get the "fName" to display itself in the "message" as when I submit it, the text comes up but there is no name when I type it into the textbox and submit it.

C#

@{
string message = "";
string fName;

fName = Request.Form["fName"];
string daySelect = Request.Form["submit"];

message = String.Format("Your first name is: {0}", Request.Form["fName"]);
}


HTML

<form method="post">
<label for="fName"></label>
First Name: <input type="text" name="fName" id="fName"
@(Request.Form["fName"]) /> <br/>

<br/><br/><input type="button" value="submit" />
<p>@message</p>

Answer

I recommend to look into ASP.NET MVC and how are models passed from controller to their views. This makes things you are doing here a lot of easier and less error prone. There are several tutorials out there.

Just looking at your HTML code a missing value attribute in the input field may be the cause. Please try

First Name: <input type="text" name="fName" id="fName" value="@(Request.Form["fName"])" /> <br/>