Get input value on server side - c#

I'm having some trouble and not sure what's going on.
I have a form with input value and want to be able to get that input value and send it back to my controller (server side).
My html code
<form action="/Home/Search" method="get">
<button class="search-btn-widget"></button>
<input class="search-field" id="sub" type="text" onblur="if(this.value=='')this.value='Search';" onfocus="if(this.value=='Search')this.value='';" value="Search" />
</form>
Then in my controller I have
string sub = Request["sub"];
However it ends up being null and not sure what's going on. Any ideas?

Just to make it work: add the name attribute
<input class="search-field" id="sub" name="sub" ...
but check this.

You need to add the name attribute to the input tag.
If you pull up the developer console and take a look at the HTTP GET request that is being sent, you will see that no query string is being associated with the request. This will let you know that the issue on the HTML side and not the ASP.Net MVC side.
Update input tag:
<input class="search-field" id="sub" name="sub" type="text" onblur="if(this.value=='')this.value='Search';" onfocus="if(this.value=='Search')this.value='';" value="Search" />
Update Controller Action to:
public ActionResult Search(string sub)

1) If you wanna see your input into the Request you must send your Form as POST:
<form action="/Home/Search" method="POST">
2) Make sure that input has a name:
<input class="search-field" id="sub" name="name"
type="text"
onblur="if(this.value=='')this.value='Search';"
onfocus="if(this.value=='Search')this.value='';"
value="Search" />
Then you will be able so see It in the request

You should add the name attribute to the input element.

Related

Paypal Return URL is not triggering properly

Our website has following one time payment buttons.
<form action="https://www.paypal.com/cgi-bin/webscr" method="post" target="_top">
<input type="hidden" name="custom" value="#Model.UserId.ToString()">
<input type="hidden" name="notify_url" value="http://www.testweb.com/paypal/IpnHandler">
<input type="hidden" name="return" value="http://www.testweb.com/paypal/Onetimeyearlysuccess">
<input type="hidden" name="cancel_return" value="http://www.testweb.com/paypal/cancel">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="hidden" name="hosted_button_id" value="8xxxxxxC">
<input type="image" src="#Url.Content("~/images/OneTime55year.png")" border="0" name="submit" style="width: 130px !important; height: 47px !important;" alt="PayPal - The safer, easier way to pay online!" >
</form>
After done the paypal payment, we could see we are getting 404 error.
The issue is that we are getting wrong URL.
https://www.testweb.com/PaypalOnetimeyearlysuccess?amt=55.00&cc=USD&cm=22028&item_name=oneTimeAnnual55&st=Completed&tx=59M0424544743135Y
We don’t know why the slash missing after paypal.
The correct URL should be
https://www.testweb.com/Paypal/Onetimeyearlysuccess?amt=55.00&cc=USD&cm=22028&item_name=oneTimeAnnual55&st=Completed&tx=59M0424544743135Y
There needs to be slash symbol after paypal in the address URL.
You can see the correct URL from the return URL of the paypal button also.
Why it is redirecting to wrong address?
Also for testing purpose,we changed the return URL of the paypal button. We simply modified to “http://www.testweb.com/paypal/Onetimeyearlysuccesstest.”
But we could see after changed the return URL , paypal is not redirecting to modified URL.
Actually we are confused ,we need to know where the return URL is creating.
PayPal only recognize the value of 'return' variable. The key point is located at this URL (http://www.testweb.com/paypal/Onetimeyearlysuccess). You are advised to create a fixed single page without redirect function like .html, .php, .aspx, etc., to see the result.

Antiforgerytoken MVC C# search form

I have a search box on my site.
The controller looks like this:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult SearchResults(SearchModel model)
{
View:
<div class="siteSearch clearfix" role="search">
<form id="HeaderSearchForm">
#Html.AntiForgeryToken()
<label for="tbSiteSearch">Search:</label>
<input type="text" id="tbSiteSearch" name="tbSiteSearch" class="text" />
<button type="submit" class="btn submit">
<i class="icon-search"></i>
</button>
</form>
</div>
So when I do a search, I can see the hidden label with the Anti Forgery Token present. This all works as expected because if I take a blank html page, copy the form code and leave the '__RequestVerificationToken' blank, I get told that the token hasn't been set and the search doesn't run. Which is what I would expect.
The issue I have is if I submit a search, copy the token from my site and place it in to my blank html page e.g.
<html>
<body>
<script>history.pushState('', '', '/')</script>
<form action="https://www.adomainname.co.uk/Search/SearchResults/" method="POST">
<input type="hidden" name="SearchTerm" value="testing" />
<input type="hidden" name="__RequestVerificationToken" value="theverificationtokengoeshere" />
<input type="submit" value="Submit request" />
</form>
</body>
</html>
The request is submitted - even if I am running the above code on localhost. I would have expected the form to not submit as it was coming from a different domain. Am I misunderstanding how this should work?
If I refresh the page and resubmit Im obviously allocated a new AFT and so the submit fails again but this doesn't feel right.
Ideally I would prefer the form action to only run on the domain it's on and not be able to execute the action from another site. I thought using AntiForgeryToken prevent Cross-Site Request Forgery.
If any of this is unclear, please let me know and I'll explain more.

Pass value from field to querystring

How do I set the value to the query string
<form asp-route="TalkMessageBoardSearch" asp-route-talk_query="[need value from "talk_query" field]" method="post" class="form" role="form">
<input type="hidden" />
<div class="input-group">
<input class="form-control" id="talk_query" name="talk_query" placeholder="Search Talk" />
<div class="input-group-btn">
<button type="submit" class="btn btn-primary"><span class="glyphicon glyphicon-search" aria-hidden="true"></span></button>
</div>
</div> </form>
When the button is clicked on POST, it is redirecting to "/message_board_search" but it doesn't have the querystring. I want it to show "/message_board_search?talk_query=query1" How do I get the value from the id="talk_query" field and put it in the querystring when the POST is clicked. Right now "asp-route-talk_query" can be hardcoded like asp-route-talk_query="hardcoded"
I used the "asp-route" because the view is in /talk/message_board_search" but I want it to be in the root path "/message_board_search" so that is why I used the route "TalkMessageBoardSearch."
You are trying to use the < form > method attribute with POST. The "method" you specify changes the behavior:
POST method does not affect the query string
GET method affects the query string.
HTTP Methods: GET vs. POST
HTML < form > method Attribute
If you need to use POST but also modify the query string to contain dynamic data, you must use Javascript. You cannot use the < form > method by itself in that case. (see Vinay's comment, he has a link for this!)

Can't get input type="text" value in action on form submit?

In a Razor view I have input type="text", a hidden field and a button. I can access hidden field from Form collection but its weird I cant access input type="text" value inside my action. I am not sure if my understanding is correct or not but I was thinking as all fields inside form should be available inside action.
Below is my code please:
#using (Html.BeginForm())
{
<div style="margin-top: 40px;">
<input id="txtDateFrom" class="span2" size="16" value="#Model.StartDate.ToString("dd/MM/yyyy") " readonly="readonly" type="text">
#Html.Hidden("currencyCode", (object)ViewBag.currencyCode)
</div>
<button onclick="#Url.Action("ExchangeRateDetails", "ExchangeRate")" class="btn btn-lg span2 ARML50px">
}
I highly appreciate your time, guidance and help.
The reason your hidden input works is that you render this with help from the Html helper #Html.Hidden. This helper render the input field with the name attribute.
Your <input type="text"> is missing the name attribute. So try writing like this:
<input id="txtDateFrom" name="txtDateFrom" class="span2" size="16" value="#Model.StartDate.ToString("dd/MM/yyyy") " readonly="readonly" type="text" />
The name="txtDateFrom" will make the value appear in your FormCollection.
Try this way
You don't need onclick="#Url.Action("ExchangeRateDetails", "ExchangeRate")" to the button
Change your button for below way
<button type="submit" class="btn btn-lg span2 ARML50px">
And Now your controller can get the input text
[HttpPost]
Public ActionResult ExchangeRateDetails(YourmodelClass xxx)
{
string dates=Model.StartDate;
}
This website have lot and lot of answers for how to send a model values from view to controllers by see the Related discussion on this page right corner .

How to pass custom data via PayPal IPN

I use following code for the RETURN URL.
#foreach (var item in Model)
{
<form action='#item.PayPalURL2 ' method='post'>
<input type='hidden' name='cmd' value='_xclick' />
<input type='hidden' name='business' value='#item.BusinessEmail' />
<input name='item_name' value='#item.Name' class="ppField" readonly="readonly"/>
<input type='hidden' name='item_number' value='#item.ID' />
<input name='amount' value='#item.Price2' class="ppField" readonly="readonly"/>
<input type='hidden' name='return' value='#item.ReturnURL' />
<input type='hidden' name='custom' value='#item.UserID' />
<input type='submit' value='Buy' />
</form>
}
Any clue how to pass custom filed to IPN message and get it via Listener?
Thank you!!!
You've already got the "custom" field included in your form. Whatever value you have in #item.UserID will be getting sent along with the payment and will come back as a POST value called "custom" within your IPN solution.
So, it looks like you're already got what you need. You just need to make sure $item.UserID actually contains what you expect it to contain, and that you're looking for the custom field in your IPN listener.

Categories