If I undertstand your question correctly, you
need to have a http post form to pass the value of the textbox to your controller.
OR you could use AJAX ( I recommend jQuery) to send the value to your controller, which does not require a form tag (but it still will issue a HTTPPOST to get the data to your controller).
However, if you do have a form on your page (your question is a little hard to understand), you can just add an additional parameter to your controller method (in addition to your model object) that matches the name of the textbox.
Please provide the code for your view and controller so we can better assist. Thanks!
New to ASP.NET? Try http://www.asp.net/get-started
New to ASP.NET MVC? Try http://www.asp.net/mvc
--------------------------------------------------------------------------------
Please click 'Mark As Answer' for the reply that assists you.
Here Save is the name of the Action and Customers as in CustomersController is the name of the Controller. And in your CustomersController you have a Save action with a parameter name:
public ActionResult Save(string name)
{
}
The model binder automagically binds the input element Name with the parameter
name.
Give a man a fish and you will feed him for a day. Teach a man to fish and you will feed him for a lifetime.
Note that the <input type="text"... needs to have a name attribute, in this case
SearchText. The model binder will match this to the parameter searchText of the
Search action, that is the reason why the parameter needs to be named exactly the same.
Give a man a fish and you will feed him for a day. Teach a man to fish and you will feed him for a lifetime.
It depends on what you need exactly. Do you just want to submit the form with jQuery or do you want to sent an asynchronous HTTP request (AJAX) using the jQuery framework?
Give a man a fish and you will feed him for a day. Teach a man to fish and you will feed him for a lifetime.
KeyurN
Member
105 Points
201 Posts
Pass textbox value to controller class
Oct 08, 2010 10:09 AM|LINK
Hi All
How to pass the textbox value to my controller there is no form and post method in the view
page.
I have taken one input type textbox which is not bind with my model.
I need to access that value to controller
Please give me a solution
Keyur N
dotNETdev09
Member
156 Points
30 Posts
Re: Pass textbox value to controller class
Oct 08, 2010 12:53 PM|LINK
If I undertstand your question correctly, you need to have a http post form to pass the value of the textbox to your controller.
OR you could use AJAX ( I recommend jQuery) to send the value to your controller, which does not require a form tag (but it still will issue a HTTPPOST to get the data to your controller).
However, if you do have a form on your page (your question is a little hard to understand), you can just add an additional parameter to your controller method (in addition to your model object) that matches the name of the textbox.
for instance, in your HTML:
<input type="text" id="Comments", name="Comments" value="" />
then in your controller:
[HttpPost]
public ActionResult Edit(Person personToEdit, string comments)
{
...
}
eccsolutions
Contributor
2320 Points
431 Posts
Re: Pass textbox value to controller class
Oct 08, 2010 02:37 PM|LINK
Please provide the code for your view and controller so we can better assist. Thanks!
New to ASP.NET MVC? Try http://www.asp.net/mvc
--------------------------------------------------------------------------------
Please click 'Mark As Answer' for the reply that assists you.
KeyurN
Member
105 Points
201 Posts
Re: Pass textbox value to controller class
Oct 11, 2010 04:24 AM|LINK
<h2>
Index</h2>
<% using (Html.BeginForm())
{%>
<table>
<tr>
<td>
Name:
</td>
<td>
<%: Html.TextBox("Name","") %>
</td>
<td>
</td>
<td>
<input type="submit" value="Search" />
</td>
</tr>
</table>
This is my view code,and i want on submit textbox value at controller
how can achive this.
Keyur N
gabriel.loza...
Contributor
3583 Points
800 Posts
Re: Pass textbox value to controller class
Oct 11, 2010 05:15 AM|LINK
In your view:
<% Html.BeginForm("Save", "Customers");%> <%: Html.TextBoxFor(m => m.Name) %> <p><button type="submit">Save</button></p> <% Html.EndForm();%>Here Save is the name of the Action and Customers as in CustomersController is the name of the Controller. And in your CustomersController you have a Save action with a parameter name:
public ActionResult Save(string name) { }The model binder automagically binds the input element Name with the parameter name.KeyurN
Member
105 Points
201 Posts
Re: Pass textbox value to controller class
Oct 11, 2010 05:45 AM|LINK
Hi
My textbox is not bind with model ,which is only use for getting the value at server side means in controller.
And this view is inherits from the IEnumreble with Employee objects so i can not get single model.
this textbox i used for search purpose so whatever textbox value filter in DB
Please help me.
Keyur N
Bober Song -...
All-Star
34686 Points
2167 Posts
Re: Pass textbox value to controller class
Oct 12, 2010 03:10 AM|LINK
Hi KeyurN ,
You can try to add Html.EndForm() to your code.
<% Html.BeginForm("Save", "Customers");%>
<div>your code</div>
<% Html.EndForm();%>
Or you can use the <form> directly.
Or you can use the jquery.form plugin for submit the input value to controller method.
http://plugins.jquery.com/project/form
http://jquery.malsup.com/form/
I hope it is helpful to you.
If you have any feedback about my replies, please contact msdnmg@microsoft.com
Microsoft One Code Framework
KeyurN
Member
105 Points
201 Posts
Re: Pass textbox value to controller class
Oct 12, 2010 04:39 AM|LINK
Hi
Below is my view code
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<script src="../../Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script type="text/javascript">
function getText() {
var URL = "/Employee/Search/" + $("#SearchText").val();
$.post(URL);
}
</script>
<h2>
Index</h2>
<table>
<tr>
<td>
Name:
</td>
<td>
<input type="text" id="SearchText" style="width: 325px" />
</td>
<td>
</td>
<td>
<%--<input type="submit" value="Search" name="Search" id="Search"/>--%>
<input type="submit" id="Search" value="Search" onclick="javascript:getText();" />
<%--<button type="submit">Search</button>--%>
<%-- <%: Html.ActionLink("Search", "Search") %>--%>
</td>
</tr>
</table>
now on the search click i want controller action to be called with SearchText value
Controller code
public ActionResult Search(string id)
{
}
But the Search action is not called what is the reason.and how to call the action result method
Keyur N
gabriel.loza...
Contributor
3583 Points
800 Posts
Re: Pass textbox value to controller class
Oct 12, 2010 04:57 AM|LINK
You don't have to make it that complex. In your view goes this:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <h2>Index</h2> <% Html.BeginForm("Search", "Employee");%> <table> <tr> <td> Name: </td> <td> <input type="text" id="SearchText" name="SearchText" style="width: 325px" /> </td> <td> </td> <td> <input type="submit" id="Search" value="Search" /> </td> </tr> </table> <% Html.EndForm();%>And in your action goes this:
public ActionResult Search(string searchText) { }Note that the <input type="text"... needs to have a name attribute, in this case SearchText. The model binder will match this to the parameter searchText of the Search action, that is the reason why the parameter needs to be named exactly the same.
gabriel.loza...
Contributor
3583 Points
800 Posts
Re: Pass textbox value to controller class
Oct 13, 2010 04:43 AM|LINK
It depends on what you need exactly. Do you just want to submit the form with jQuery or do you want to sent an asynchronous HTTP request (AJAX) using the jQuery framework?