Asp.net MVC add textbox value to routelink

1.3k Views Asked by At

I am trying to learn asp.net MVC and having an issue to submit a textbox value to a model.

I have a text box in which users will type a number and when they hit the routelink the routelink will take the value from the textbox and assigns it to one of .Page item.

<%=Html.TextBox("PageIndex")%>
<%=Html.RouteLink("Search", "UpcomingEvent", 
    New With {.Page = "I want to put value of PageIndex textbox here"})%>

How can I assign the value of the textbox to .Page variable? Thanks for your time and help!

2

There are 2 best solutions below

0
On BEST ANSWER

You can't do that because the RouteLink gets rendered on the server.

If you want to construct a URL based on the user input without a postback, you'll need to do some client-side scripting (ie JavaScript).

3
On

It sounds like you're not expecting to post back to the server once they have the textbox value entered. If that is the case then you're going to need to use javascript to change the link's href property. Html.RouteLink is all done server side.

If you are using jquery then it would be something like

$("#pageIndex").change(function()
  {
    $("#pageLink").href += "?pageIndex=" + $("#pageIndex")"
  }

Of course that isn't going to work with multiple change events being fired but that part is left as an exercise for the reader.