I'm trying to append data from a form to the Url of my page using Razor (adding a query string). I've done this in ASP.NET Web Controls okay, but I would prefer to do this in Razor if it is possible?
Here's a very basic version of my Razor script but currently the '#test' variable is empty on post:
#{
<form id="test" method="post">
<input type="text" />
<input type="submit" value="Submit" />
</form>
if(IsPost){
var test = Request.Form["test"];
Response.Redirect(#Model.Url + "?test=" + #test);
}
}
As a sidenote, is there a way of achieving this without a POST method at all?
As I understand you want to add input value in your test variable. You should define id for input[text] or you should change it to
Page:
#using (Html.BeginForm("ActionName", "ControllerName", FormMethod.GET, null))
{
<input type='text' name'test' id='test' />
<input type="submit" value="Submit" />
}
or your code
#{
<form method="get" action="#Model.Url">
<input type="text" name="test" id="test" />
<input type="submit" value="Submit" />
</form>
}
P.S. I'm not clearly understand your code, because you set POST method and want to process GET method.
Related
I am struggling with the following problem. I have in my .Net 4 application with Razor Page a database query that is started with a button (upper part in the code) and now I want to send the value of the radio button group (lower part in the code) with it, is this feasible and if so how? I would like to do this, if somehow possible, only with one button. Maybe this is also possible with a ViewBag variable?
How would you do that?
Many thanks in advance.
<form method="post" id="queryForm" asp-controller="HomeController">
<div class="input-group">
<select id="sqlQuerriesDropDown" class="form-select" name="selectedQuery">
#foreach (var SqlQuery in ViewBag.SqlQuerriesDropDown)
{
<option value="#SqlQuery.getQueryName()">#SqlQuery.getQueryName()</option>
}
</select>
<p> </p>
<!-- Schaltfläche Anzeigen -->
<input type="submit" class="btn btn-primary" value="View result"/>
<p> </p>
</div>
<div>
<br />
<label><b>Decimal separator</b></label><br />
<label><input class="boost_radio" onklick="boostNo(1)" asp-for="Boost_No.Number" type="radio" name="mySeperator" value=0 ViewBag.RB="0" checked="checked" /> Comma</label><br />
<label><input class="boost_radio" onklick="boostNo(2)" asp-for="Boost_No.Number" type="radio" name="mySeperator" value=1 ViewBag.RB="1" /> Point</label><br />
<span asp-validation-for="Boost_No.Number"></span><br />
<input type="submit" value="submit" name="myDelimitter" id="submit" />
</div>
</form>
This codes should help you. Please replace your controller names.
public ActionResult Index(FormCollection form)
{
string[] optionArray = Request.Form.GetValues("selectedQuery");
return View();
}
A last point is I strongly recommended to use models instead of viewbag.
i am working with Atom Payment Gateway and done with the payment part and now i am working with refund part in the documentation they suggested
i have to use form and action is used as the URL given by Atom for e.g
<form action="https://paynetzuat.atomtech.in/paynetz/rfts" method="POST">
<input type="hidden" name="merchantid" value="197" />
<input type="hidden" name="pwd" value="VGVzdEAxMjM=" />
<input type="hidden" name="refundamt" value="200.00" />
<input type="hidden" name="txndate" value="2019-01-08" />
<input type="hidden" name="merefundref" value="123" />
<input type="hidden" name="atomtxnid" value="25631" />
Submit : <input type="submit" name="submit" value="Submit" />
</form>
but in my project i have to implement this with mvc using razor form i have to use controller to get the info of refundamt, currentdate and merefundref
if i use demo hidden values then i am getting the response
but in my project i have to use controller method to get this values then post to that given url.
in the documentation they have given that the method should compulsory be HttpPost without that the Url will not give expected response.
in my project i have used the razor form with controller method and http POST method then i am going to that method but not able to post the above values to that particular url
Here is my razor form
#using (Html.BeginForm("Index", "Refund", FormMethod.Post))
{
<input type="hidden" name="merchantid" value="197" />
<input type="hidden" name="pwd" value="VGVzdEAxMjM=" />
<input type="text" name="atomtxnid" value="100004487215" />
<input type="hidden" name="refundamt" value="200.00" />
<input type="hidden" name="txndate" value="2019-01-08" />
<input type="hidden" name="merefundref" value="25631" />
<input type="submit" name="submit" value="Submit" />
}
Here is my controller method
[HttpPost]
public ActionResult Index()
{
return Redirect("https://paynetzuat.atomtech.in/paynetz/rfts");
}
In normal Html form i am using action as that given url and passing values with hidden and text field but in my case i want to post values through the controller method to that url please help me with this 'https://paynetzuat.atomtech.in/paynetz/rfts' url
Please suggest me a way to post the above values through controller method to this
if you are just gonna pass the values first two null for the controller and action of your project, #action = .... is the part for the url you want to post
#Html.BeginForm(null, null, FormMethod.Post, new {#action="https://paynetzuat.atomtech.in/paynetz/rfts"}
<input type="hidden" name="merchantid" value="197" />
<input type="hidden" name="pwd" value="VGVzdEAxMjM=" />
<input type="text" name="atomtxnid" value="100004487215" />
<input type="hidden" name="refundamt" value="200.00" />
<input type="hidden" name="txndate" value="2019-01-08" />
<input type="hidden" name="merefundref" value="25631" />
<input type="submit" name="submit" value="Submit" />
)
but if you want to do something first to the hidden values passed, catch it first in the method in
//all of the values you will post is
//acceptable in string,
//so i will just use string, but it will depend on you
//if there is a type you want to use.
[HttpPost]
public ActionResult Index(string merchantid, string pwd, string atomxnid, string refundamt, string txndate, string merefundref)
{
//do something
//return it to a view as a model
//do step above with html.hiddenfor(model => model.prop1/prop2);
return View(model);
}
I have two methods in my asp.net mvc controller which search the google maps Places API to perform an autocomplete action as the user types in text.
In the background I understand that the data-otf-autocomplete function uses JQUERY UI autocomplete in the background to call these methods on the form. I have to be honest and admit I don't fully understand the JQUERY UI piece as I have adapted it from another the OdetoCode sample from the AJAX lesson on Scott Allen's excellent Pluralsight course Building Applications with ASP.NET MVC4 (Great course btw). The original code from Scott's course linked to a local database to return JSON to complete an autocomplete function which I've adapted to connect to the Google MAPS Places API.
Right now I have two search boxes each linked to a different methods on the controller. One uses the locality search (best suited to approximate addresses, most rural addresses here in Ireland fall into that category) while another is suitable for exact street addresses.
Is it possible to wire these two up to a radio button control in my view so you have a single search box and depending on the radio button option selected (say locality/address) it automatically routes the request to the associated method controller
#model IPagedList<RestaurantListViewModel>
#{
ViewBag.Title = "Home Page";
}
<form method="get" action="#Url.Action("Index")"
data-otf-ajax="true" data-otf-target="#List">
<input type="search" name="searchTerm" data-otf-autocomplete="#Url.Action("Autocomplete_Address")" />
</form>
<form method="get" action="#Url.Action("Index")"
data-otf-ajax="true" data-otf-target="#List">
<input type="search" name="searchTerm" data-otf-autocomplete="#Url.Action("Autocomplete_Locality")" />
</form>
#Html.Partial("_Restaurants", Model)
Cant you just have the search box and 2 radio buttons beside it one for each search option.
<input type="radio" name="SearchType" value="local" checked>Locality
<input type="radio" name="SearchType" value="exact">Exact Address
You just need one form then, posting to a method in the controller, in the method check for the value of the radio button and run your logic based on this.
UPDATE
Razor View:
#using (Html.BeginForm("ControllerMethod", "YourController", null, FormMethod.Post))
{
<input type="radio" name="SearchType" value="local" checked>Locality
<input type="radio" name="SearchType" value="exact">Exact Address
}
ControllerMethod
[HttpPost]
public ActionResult ControllerMethod(FormCollection form)
{
var SearchType = form["SearchType"];
........
}
Fully working code using Stephen Muecke's suggestion
#model IPagedList<RestaurantListViewModel>
#{
ViewBag.Title = "Home Page";
}
<script type = "text/javascript" >
function ShowHideDiv() {
var chkAddress = document.getElementById("chkAddress");
var inputAddbox = document.getElementById("inputAddress");
inputAddbox.style.display = chkAddress.checked ? "block" : "none";
var chkLocality = document.getElementById("chkLocality");
var inputLocality = document.getElementById("inputLocality");
inputLocality.style.display = chkLocality.checked ? "block" : "none";
}
</script>
<form method="get" action="#Url.Action("Index")"
data-otf-ajax="true" data-otf-target="#restaurantList">
<div>
<input type="search" id="inputAddress" name="searchTerm" data-otf-autocomplete="#Url.Action("Autocomplete_Address")" checked/>
<input type="search" id="inputLocality" name="searchTerm" data-otf-autocomplete="#Url.Action("Autocomplete_Locality")" />
</div>
<label for="chkAddress">
<input type="radio" id="chkAddress" name="chkType" onclick="ShowHideDiv()" />
Search by Address
</label>
<label for="chkLocality">
<input type="radio" id="chkLocality" name="chkType" onclick="ShowHideDiv()" />
Search by Location
</label>
</form>
#section Scripts {
#Scripts.Render("~/bundles/jqueryval")
<script language="javascript">
$(document).ready(function () {
$("#inputLocality").hide();
$("#chkAddress").prop('checked', true);
});
</script>
}
I'm trying to post some information from a form but it keeps posting null values to the Comment controller rather than what is entered on the page....
#using (Html.BeginForm("SubmitComment", "Comment",FormMethod.Post))
{
<fieldset>
<legend>Submit a comment</legend>
<input id="comment" type="text" />
<button type="submit">Submit</button>
</fieldset>
}
Any ideas?
Add the name="comment" attribute to the input.
I am working in C#(asp.net). I have two pages 'abc.aspx' and 'xyz.aspx'. I want to send data from 'abc.aspx' to 'xyz.aspx'. I am using this code.
In 'abc.aspx'
<form action='xyz.aspx?site=google&code=123' method='get'>
<input type='text' name='name1' />
<input type='submit' value='submit' />
</form>
Now, I want to access all three values (site,code and name1). But, in 'xyz.aspx', I got only one value i.e name1. How to get all three values.
You need to put the values into hidden <input /> elements and hard-code the values if you want to have them end up in the query string. You're correct in setting the method='get':
<form action='xyz.aspx' method='get'>
<input type='hidden' name='site' value='google' />
<input type='hidden' name='code' value='123' />
<input type='text' name='name1' />
<input type='submit' value='submit' />
</form>
I think this one is the best.
In abc.aspx
<form action="xyz.aspx?site=google" method="post">
<input type="text" name="name1" />
<input type="submit" value="Submit" />
</form>
In xyz.aspx, access the data like this..
string site = Request.QueryString["site"];
string name = Request.Form["name1"];
//Remaining code...