.Net MVC Razor submit form post action - asp.net-mvc

I have an html form like:
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Index</title>
</head>
<body>
<form name="mercForm" action="http://someurl.com/" method="post">
<input type="hidden" name="some_input" value="206">
<input type="submit" value="sub" />
</form>
</body>
</html>
When I post this form by clicking submit button, the new window opens with the posted values. But, I want to implement the same form into a MVC Razor page. I habe tried some code but I failed. New window does not open.
#using (Html.BeginForm("http://some url.com/","SomeController", FormMethod.Post, null))
{
<input type="hidden" name="some_input" value="206">
<input type="submit" value="sub" />
}
How can I make it work?

As I know Html.BeginForm does not take 1st argument as some url, it should be some Action name.
If you want to post your form data to some third party web site then just collect the data inside an action directly and process the form post using HttpWebRequest.

Related

Passing a parameter to a webhook

Want to pass and retrieve a query string to my twilio webhook.
http://xx.xx.xxx.xx/TwilioWebhookTraffic/TwilioWebHookTraffic.aspx/TrafficCallBack?GCAcctNbr=667118358011603
which returns:
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<form method="post" action="./TrafficCallBack?GCAcctNbr=667118358011603" id="form1">
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="4PSLlcjmwjYT0iXthm7eB3EX5v8lZY62GKDxWDdNDuZndwHLxahDzcX2H0NFNQX+2NAe7hPownCNYxJJVZJreGwJpqZ/Cwm3f2EIiLGFQ4c=" />
<input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="CFB6114E" />
<div>
TWILIO TRAFFIC REMINDER WEBHOOK ASPX PAGE</div>
</form>
</body>
</html>
But does not run the webhook code.
if the URL is just:
http://xx.xx.xxx.xx/TwilioWebhookTraffic/TwilioWebHookTraffic.aspx
then the code is run. but, of course I need the query string.

How to get form data in Java Spark using Thymeleaf template engine?

I've built a simple application and now I'm trying to host attach it to a web server. I'm attempting to have a HTML form (using Thymeleaf) that the user enters their location in as text, and then my server will take and produce a result using that string. So to get started, I'm attempting to make a simple spark application that makes a home page with a "enter your location" form, that then gets the users input and does something with it. I can get the "entryMessage" displayed, as tutorials show, but how to get user data is proving difficult.
However, there is very little documentation on how this can be done with these two framworks. My attempt at what the code should look like is as follows. Note the middle post is just me trying to find ways to get the form data - none proved succesful
ThymeleafTemplateEngine engine = new ThymeleafTemplateEngine();
HashMap<String, String> userLocationMap = new HashMap<>();
get("/home", (request, response) -> {
userLocationMap.put("entryMessage", "Please enter your location");
return new ModelAndView(userLocationMap, "home");
}, engine);
post("/home", (request, response) -> {
System.out.println(request.toString());
//System.out.println(request.body());
//System.out.println(userResponse.location);
//response.redirect("/locationAccepted");
return userLocationMap.get("userLocation");
});
get("/locationAccepted", (request, response) -> {
String location = request.queryParams("userLocation");
return new ModelAndView(userLocationMap, "locationAccepted");
}, engine);
with the following thymeleaf templates
home.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p> <span th:text="${entryMessage}"> default message </span> </p>
<form action="/locationAccepted" method="post">
<input type="text" th:field="userLocation"/>
<div class="button">
<button type="submit">Send your message</button>
</div>
</form>
</body>
</html>
and locationAccepted.html
<!DOCTYPE html SYSTEM "http://www.thymeleaf.org/dtd/xhtml1-strict-thymeleaf-4.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p> <span th:text="${userLocation}"> default message </span> </p>
</form>
</body>
</html>
You have two bugs in your code, both in the HTML form:
In your Java code you're defining the route "/locationAccepted" as GET, but your form method attribute is POST => Change your form to GET.
If you want to get the form's input data it should have a name with value userLocation. th:field isn't translated to name (it's translated to field attribute which I'm not sure what it means).
So your form (after Thymeleaf) should look like this:
<form action="/locationAccepted" method="GET">
<input type="text" name="userLocation"/>
<div class="button">
<button type="submit">Send your message</button>
</div>
</form>
And then request.queryParams("userLocation") will work like you wanted.

How to prevent submit buttons not to show the url in the address bar?

Folks,
I just noticed something interesting developing my first ASP.Net Mvc application and Its when you mouse over on a submit button in Mozilla the full Url is shown in the status bar! Is it a normal behavior?! how can i prevent that?
Try the following code:
<html>
<body>
<form id="myForm">
<input type="text">
<!-- <input type="submit" value="Send" > -->
<input type="button" value="Send" onclick="onClick(this)">
</form>
<script type="text/javascript">
function onClick (e)
{
document.forms["myForm"].submit();
}
</script>
</body>
</html>

Why Does IsPostBack Require runat="server" on the Form?

I must not understand something fundamental about the aspx page processing cycle. Please take a look at this simple example below.
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<div>
<form method="post">
<textarea name="someContent" cols="35" rows="15"></textarea>
<input type="submit"/>
</form>
</div>
</body>
</html>
<script runat="server">
public void Page_Load() {
// The httpMethod is always set correctly to "GET" or "POST"
String httpMethod = HttpContext.Current.Request.HttpMethod;
if(IsPostBack)
DoSomething();
else
DoSomethingElse();
}
</script>
Note the <form> element does not have a runat='server' attribute.
When the page is loaded for the first time, the Page_Load() fires and the httpMethod variable is set to "GET" and the IsPostback property returns false, all as expected.
When the user clicks the "submit" button, the Page_Load() fires again and the httpMethod variable is set to "POST", so the ASP.NET plumbing obviously knows this is a POST verb; however, the IsPostBack property still returns false. This seems odd to me. I would think that if the httpMethod was set to "POST", the IsPostBack would return true.
If I change the <form> element to contain a runat='server' attribute things change a bit. Now when the user presses the "submit" button the httpMethod variable is set to "POST", just as before, but now IsPostBack returns true.
Since I have no need to access the <form> element on the server, I saw no need to use a runat='server' attribute on it. But for some reason, the runat='server' must be present on the <form> in order for the IsPostBack to return a correct value, even though the HttpContext.Current.Request.HttpMethod property returns the correct value regardless of the runat='server' attribute.
Can anyone explain WHY the runat='server' is necessary on the <form> to make IsPostBack work correctly?
NOTE: Please note that I am not asking how to "do this" or "do that". My goal is to understand "The Why".
Thanks
Page checks few special fields (viewstate and postback event) to determine if request is postback or not.
http://referencesource.microsoft.com/System.Web/R/ae07c23d0aba6bb9.html
Both forms here cause IsPostBack to be true:
<%# Page Language="C#" AutoEventWireup="true" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<p><%= IsPostBack? "POSTBACK" : "NO POSTBACK" %></p>
<form id="form1" runat="server">
<input type="submit" />
</form>
<form id="form2" method="get">
<input type="hidden" name="__EVENTTARGET" value="" />
<input type="submit" />
</form>
</body>
</html>
Server-controlled form just adds and populates these fields automatically.

Form reloads the page but does not submit

I'm working on Asp.net MVC Application. When I use a form and try to submit , it just reloads the page. I have included the method and Action info , but the form and data are not sent to address specified in action attribute of form. Any help please
<form action="/Search/SearchforQuery/" method="post" runat="server">
<p>Search : <input style="font-size:medium; width: 600px; height: 29px;"
name="searchField" id="searchField" /></p>
</div> <input type="submit" value="Submit Form" />
</form>
Why are you using runat="server" if you are using MVC?
Delete it.
Try to use:
#{ using (Html.BeginForm(...))
{
<p>
Content here
</p>
}

Resources