I have a simple site with just 1 page that has a form with just 1 field and a submit button.
It asks for a phone number and calls a stored procedure with the value of the phone number captured in the form. Once submitted, it feedbacks the user saying that the number was submited, and it stays on the page so the user can enter another phone number.
So I created a cshtml file that looks like this:
@{
string pn=Request.Form["pn"]
and then my code to call the stored procedure with pn as a parameter
}
The problem I am having is that when I go to this page the server side throws an error saying that the stored proc expects a parameter that was not supplied.
Therefore, I want to create a JavaScript function that will run onload of the page that will only call the stored proc if pn has a value.
How do I do that, and where should I place the code (JS inside the @{} block?)
BTW, as of now, I have 2 pages. The first one, without any server side code, and the second one which is a copy, plus the server side code.
In the first place you don’t need a JavaScript function to validate your input client side, but a server side code to test if the form is submitted and if there is a valid value into your field.
Something like:
If you want to deepen this subject, look at this tutorial: Introduction to Working with a Database in ASP.NET Web Pages (Razor) Sites.