CSHTML displaying user inputs - c#

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>

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/>

I found the problem.
Here is the submit that I have in:
<br/><br/><input type="button" value="submit" />
As you can see the type is button, for this code it should be of type "submit", tiny error in lots of code.
Thanks to rboe for the assistance.

Related

HTML Forms in ASP.NET Web Pages gives errors CS0103 & CS1061

I have no previous experience with C# and .NET so I'm following this tutorial to create an HTML form for some practice.
It says on the page, At the top of the Form.cshtml file, enter the following code:
#{
if (IsPost) {
string companyname = Request.Form["companyname"];
string contactname = Request.Form["contactname"];
int employeecount = Request.Form["employees"].AsInt();
<text>
You entered: <br />
Company Name: #companyname <br />
Contact Name: #contactname <br />
Employee Count: #employeecount <br />
</text>
}
}
However, no matter where I place this piece of code on the page it gives me errors on (IsPost) (CS0103), that The name 'isPost' does not exist in the current context and AsInt() (CS1061), 'StringValue' does not contain a definition for 'AsInt' and no accessible extension method 'AsInt' accepting a first argument of type 'StringValue' could be found (are you missing a using directive or an assembly reference?)..
I'm not familiar with C# syntax but I thought this would be simple since it's literally like two steps in the tutorial, but somehow, I manage to fail with it anyways. I've Googled the errors and like mentioned I've tried to place the code snippet above in different places, but I have a hard time seeing still why it does not work.
Below is the code in full. Someone with more knowledge that can see why this isn't working?
#page
#model IndexModel
#{
ViewData["Title"] = "Home page";
}
#{
if (IsPost)
{
string companyname = Request.Form["companyname"];
int employeecount = Request.Form["employees"].AsInt();
<text>
You entered: <br />
Company Name: #companyname <br />
Employee Count: #employeecount <br />
</text>
}
}
<!DOCTYPE html>
<html>
<head>
<title>Customer Form</title>
</head>
<body>
<form method="post">
<fieldset>
<legend>Add Customer</legend>
<div>
<label for="CompanyName">Company Name:</label>
<input type="text" name="CompanyName" value="" />
</div>
<div>
<label for="Employees">Employee Count:</label>
<input type="text" name="Employees" value="" />
</div>
<div>
<label> </label>
<input type="submit" value="Submit" class="submit" />
</div>
</fieldset>
</form>
</body>
</html>
The tutorial you are following is for Asp.Net Framework 4.X as indicated at the top of the tutorial page. Specifically, Asp.Net Web Pages Razor 3. However, you have most likely created a Project for Asp.Net Core (6) which does not support the IsPost property. This may most likely be why the compiler is giving you the error message. To fix that error, you might be able to substitute if(Model.FormPosted) to check to see if the form has been submitted. However, you may want to take a step back start by establishing which version of Asp.Net you want to learn. If you have a specific need to learn Framework 4.X then you probably want to open Visual Studio and choose File>New Project and find the Asp.Net Framework 4.X template and start over with that tutorial you had been following.
But if you are primarily wanting to learn how to create a new Asp.Net website, then you may want to start with Asp.Net Core as it is the newer technology meant to replace Framework 4, and is being more actively developed by Microsoft.
Here is the documentation for Asp.Net Core
Solution to error CS0103:
I think isPost is not a method of Razor pages (?), which seems to have caused the error. After more Googling around it worked by changing if (isPost) to the following:
if (HttpMethods.IsPost(Request.Method))

ASP.NET Saving Data

So I have a basic crud that displays information on ASP.Net using entity framework. When i click details it shows the information for that row on a table. I've input a button and a label that when is clicked shows a number, when clicked again it will show the next number higher. It's basically to just a button counter written with JQuery. My question is, is there a way to save this number? I want to store the data in the program so it doesn't forget the number when I move to a different page.
Here is the JQuery.
var count = 0;
function Count() {
count++;
$('#lblShow').text(count);
}
<div>
<input type="submit" name="btnCount" value="Add Signature" onclick="return Count();" id="btnCount" /> <input type="submit" name="btnCountSoc" value="Post to Social Media" id="btnMedia" />
<span id="lblShow"></span>
</div>
Any advice would be appreciated, thanks.
You can use cookies like :
var counter = $("#foo").val();
document.cookie = "counter="+counter";path=/";

Saving a search string to use in other cshtml pages in ASP.NET Core Web Application

I am trying to save input (an address) from an html form in order to use in other cshtml pages in my asp.net core web application. I am also using Razor as a template language. I have a web application in which the user will enter an address on one page and then on another page I want to use that input and place it into an api call. My html form is below.
<form method="post" action="/Features">
<p>
Address: <input type="text" name="searchString"/>
<input type="submit" value="Search" />
</p>
</form>
I have previously used sessions but it was for an asp.net project using web forms (I believe) and am not sure if this is the route I should go. The address being entered doesn't need to be kept secure. Thanks for any help.
You have two options here:
Submit the form to your page, store the address in TempData then redirect to the second page where you would use your submitted value (Here you use the Post/Redirect/Get pattern in order to avoid your form being submitted again by mistake via a page reload).
Submit the form directly to the second page.
Going with the first option is recommended.
Page:
<form method="post" asp-page-handler="Features">
<p>
Address: <input type="text" name="searchString"/>
<input type="submit" value="Search" />
</p>
</form>
Handler:
public IActionResult OnPostFeatures(string searchString)
{
TempData["Key"] = searchString;
return RedirectToPage("/SecondPage");
}
Then on your second page you get the searchString in the same way via:
string value = TempData["Key"] as string;
As i said you can also submit your form to the second page where you can do what ever you want with your value. Just be careful of multiple submissions.
Here is a demo to use TempData in Razor page:
cshtml:
<form method="post" asp-page-handler="Features">
<p>
Address: <input type="text" name="searchString"/>
<input type="submit" value="Search" />
</p>
</form>
cshtml.cs:
public IActionResult OnPostFeatures(string searchString) {
TempData["searchString"] = searchString;
xxxxxxx
}
Other Page you want to use searchString:
string searchString=TempData["searchString"];
//TempData.Keep(); can help you keep the value,and you can still use the searchString value in other place
TempData.Keep();
or you can use
string searchString=TempData.Peek("searchString");
So that you can still keep the searchString value

MVC razor form ONLY submits when last DropDown is populated

I actually found a solution. It was trying to run validation. But only the last validation was added. The message for validation was not displaying.
I can NOT figure this out. I have a form that is created using razor like this
#using (Html.BeginForm("Search", "WellSearch", FormMethod.Post))
{
#Html.Partial("_DropDownList_Operator", Model)
#Html.Partial("_DropDownList_Lease", Model)
<input type="submit" id="buttonClearAllFields" class="btn btn-small submit" name="Command" value="ClearAllFields" />
<input type="submit" id="buttonSearch" class="btn btn-small submit" name="Command" value="Search" />
}
What is wired is this I can ONLY submit form when the last DropDown is populated with a value. If I leave both drop downs blank can't post to a controller. Also if I switch the order of the drop downs around make Operator last it is the same thing.
I my actual code I have 10 total drop down boxes the same thing only the last one is populated. I know MVC pretty well but not an expert.
My question is this what should I check to troubleshoot this behavior. I check everything no errors, no exceptions. Not sure how to troubleshoot this need an advice what to be aware of or maybe someone was in a same situation.
This is a code for a partial view
#model [removed].Models.[removed]
#Html.TextBoxFor(a => a.[removed].LeaseKey, new { id = "lease"})
[removed] - removed those because it contains personal information
I actually found a solution. It was trying to run validation. But only validation for lease drop down was added. The message for validation was not displaying.

Reddit bot - either bypass or click "yes" to over 18 check?

I am making a Reddit comment bot that will crawl subreddits as it finds links in the pages. My problem is that when I try to crawl NSFW subreddits with an 18+ age question, despite clicking "yes" in my normal browser, Reddit returns a 18+ age question every time the C# bot hits those pages.
Is there a way to avoid this or to click the "yes" button programatically?
yes there is a way, look at the HTML form for the question:
<form method="post" action="" class="pretty-form">
<input type="hidden" name="uh" value="">
<p>are you over eighteen and willing to see adult content?</p>
<p>
<button type="submit" name="over18" value="yes">yes</button>
<button type="submit" name="over18" value="no">no</button>
</p>
</form>
You could fill out the values and then submit the POST request.

Categories

Resources