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!)
Related
let me explain a problem first.
I've got a page.
Page is basket representation with multiple records (items).
Every item has a delete button. What I want to achieve is to delete item from that basket (actually from database). But first I need to pass item id to page-model post method.
Right now that is what i got at Page view.(cshtml, dont really sure how to call it)
<form method="post">
<div class="form-group">
<input type="submit" class="btn btn-danger" value="Delete" asp-page-handler="Delete"/>
</div>
</form>
And here is a page model.
public IActionResult OnPostDelete(int itemId)
{
memberData.DeleteFromBasket(itemId);
return RedirectToPage("./Basket");
}
How can i pass item id to the method?
You need to add a route parameter to the button that matched the parameter name. For example:
<input type="submit" class="btn btn-danger" value="Delete"
asp-page-handler="Delete"
asp-route-itemId="#model.ItemId" />
I am passing the input data from the .cshtml page to the action method.
Here is my .cshtml page:
#model passingdata
<form asp-controller="Home" method="post" asp-action="About" >
<button class="btn-danger" type="submit" value="Submit"></button>
<div class="form-group">
<label>
<input asp-for="date" placeholder="Date" class="col-md-8" />
</label>
<label>
Select from these Four Classes
<input type="radio" name="class" asp-for="classselect1" id="classselect1" value="classselect1" class="col-md-4" /> <p> #Model.classname1</p>
<input type="radio" name="class" asp-for="classselect2" id="classselect2" value="classselect2" class="col-md-4" /> <p> #Model.classname2</p>
<input type="radio" name="class" asp-for="classselect3" id="classselect3" value="classselect3" class="col-md-4" /> <p> #Model.classname3</p>
<input type="radio" name="class" asp-for="classselect4" id="classselect4" value="classselect4" class="col-md-4" /> <p> #Model.classname4</p>
</label>
</div>
</form>
And here is my controller code which is invoked when i Click on the button.
[HttpPost]
public IActionResult About(passingdata p)
{
ViewData["Message"] = "Your application description page.";
Teacher.classselect1 = p.classselect1;
Teacher.classselect2 = p.classselect2;
Teacher.classselect3 = p.classselect3;
Teacher.classselect4 = p.classselect4;
Teacher.date = p.date;
return View();
}
The input data like date and bool value from the radiobutton is not passing through the object of the class which contain these variables.
Please help me in this.
If i remember correctly, the Name attribute of each radiobutton is how .net MVC will map the values to your model.
In this case, all of your names are "class", which essentially means you have 1 form field named class with 4 options.
I would recommend using the html helper classes, because they will automatically create the proper html for you. The answer to this post should help: When using .net MVC RadioButtonFor(), how do you group so only one selection can be made?
If you dont want to use the helper just remember that when you submit a form, the data that is posted is based on the name of each form field. .Net does some magic in the background to serialize your Model for you, but essentially you are just submitting data in the format "?prop1=val1&prop2=val2".
Update
I figure maybe I should clarify a little better why what you are doing is not working how you expect.
When you post or put data via a form, it passes the input fields (text box, radio button, checkbox, etc...) as either querystring params or are part of the body. Radio buttons work a little differently than other input type. For a radio button, there are multiple input elements, but only one of them is valid. That is handled by using the name attribute. In your case, all of the names are "class", which means that the only thing being passed to the server is a single "?class={val}" (val is the value of which ever radio button is selected).
If your passingdata model had a property called "class", it would be populated. If your goal is to populated all 4 of the classselect properties with different values, you would need the name of each radio button to be different. But if there was only one radio button with each name, then each property could only have 1 value. You would need multiple RadioButtons with the same name to have multiple values (only one of which is selectable for each property).
Hopefully that clarifies what is wrong and gets you in the right direction.
I am using ascx page. which I am injecting in UMBRACO.
following is my .ascx Code.
<form id="main-contact-form" name="contact-form" method="post" action="#">
<div class="form-group">
<input type="text" runat="server" name="name" id="txtName" class="form-control" placeholder="Name" required />
</div>
<button type="submit" class="btn-primary">Send Message</button>
</form>
I have try to get value of text box (txtName) from back end but I am not abe to access it.
I have tried following method to do so..(Where strtxtName is a string variable)
1) strtxtName = txtName.Value
2) strtxtName = Request.QueryString("txtName")
3) Request.Form.AllKeys -> return Nothing.
But not able to get value of text Box ,
I don't think Umbraco is making any issue.?
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.
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 .