0

I want to pass the Html.Textbox value to a controller from anchor tag, so that I can search the value passed to a controller. Please tell me how can I achieve this.

<a  href="@Url.Action("Index", "Home", new {  })">@p</a>

public ActionResult Index(string String)
        {


        }

@Html.TextBox("String")

3 Answers 3

3

use jquery

@Html.TextBox("String", null, new { @class="txtString" })
<a  href="@Url.Action("Index", "Home", new {  })" class="linkAction">@p</a>

then in your script

$('.txtString').on('blur', function(){
    $('.linkAction').attr('src', '@Url.Action("Index", "Home", new { text = "----" })'.replace("----", $('.txtString').val()));
});
Sign up to request clarification or add additional context in comments.

2 Comments

it shows { class = txtString } in a text box. Is there any mistake in syntax.
sorry put the class into the object values instead of the html values. added a null for the object values so it should render correctly now
2

You don't have to use jQuery. If you're doing a HttpPost, you just need the "name" of the textbox.

On your page:

@using (Html.BeginForm("Index", FormMethod.Post)) {
    @Html.TextBox(string.Empty, new { name = "textbox" })

    <input type="submit">Submit</input>
}

Then in your controller:

[HttpPost]
public ActionResult Index(string textbox) {
    // The name of the string parameter must match the name given to the TextBox element on the page.
}

3 Comments

Can i pass the name with the help of anchor tag as i pass page number as below : <a class="name" href="@Url.Action("Index", "Home", new { page = p })">@p</a>
For that, you'd have to use jQuery, and Matt Bodily's answer. Using a form like this is really a more of an accepted way of doing things, since you're looking to retrieve dynamic information from the client on the server. It all depends on how you're using it, and which convention you want to follow.
string.empty should be string.Empty with a captital: E
0
@using (Html.BeginForm("Index", "Home", null, FormMethod.Post, new {@id = "my-form"}))
{
    @Html.TextBox("String")
}

<a href="javascript:document.getElementById('my-form').submit();>@p</a>

You can use FormMethod.Post or FormMethod.Get. The latter will append ?String= to the url.

Comments

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.