3

I want to know is it possible to find out the value of html.textbox inside a view. if i have @Html.TextBox("textdata") can I read data from that textbox like in my paragraph

my value is: **

So i need this becouse i want that user write a number inside a textbox which i will take as an param to my function like: @Html.ActionLink("click me", "actionname", "controller", new { param = textbox value}, "")

1
  • You'll probably have to use Javascript for that. Can give you an example if you want? Commented May 16, 2011 at 8:13

1 Answer 1

7

You need to use javascript for this. Instead of using an action link a better way to achieve this would be to use a form:

@using (Html.BeginForm("actionname", "controller", FormMethod.Get))
{
    @Html.TextBox("textdata")
    <input type="submit" value="click me" />
}

This way the value entered by the user in the textbox will be automatically sent to the server when he submits the form.

If you still want to do this using javascript (not recommended) here's how you could proceed with jQuery. Subscribe for the click event of the link and fetch the value from the text field and append it to the url:

$(function() {
    $('#id_of_your_link').click(function() {
        var value = $('#textdata').val();
        $(this).attr('href', function() {
            return this.href += '?param=' + encodeURIComponent(value);
        });
    });
});
Sign up to request clarification or add additional context in comments.

5 Comments

I have already cretae my action inside controller and I just want to make this simple adjustment and send a value from textbox. Is there way to show data from textbox inside a paragraph?
@GoranB, as I explained in my answer you need to use javascript to achieve this if you want to use an ActionLink. If you follow my advice with the form you don't need javascript. The value will be automatically sent to the controller action.
Thanks, I set my controller to recieve a int value and i thought it would be great if I could send value from textbox to it. That would save me lot of time and wories, but i think tha i can use a javascript to get value and send it to my action. I mean all I have to do is write param to function and i stuck there
to take value from ropdown would be easier? i need that my user select value and to take that value and send it to my action? is it possible?
@GoranB, I repeat: if you use a form all values that the user enters in fields inside this form (drop downs, textareas, text fields, checkboxes, ...) will be automatically sent to the server when the user submits the form. If you don't use a form you will have to write javascript as shown in my answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.