views:

215

answers:

2

Hi Everyone!

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!

+3  A: 

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).

çağdaş
+1  A: 

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.

stimms
`pageIndex` might be a route value rather than a QueryString key though, which makes it a bit more complicated.
çağdaş
Not really, the route values are still passed back to the server in the URI
stimms
stimms, Imagine his URL is `/home/search/2` where `2` is the pageIndex value. With your snippet, the URL would become `/home/search/2?pageIndex=3`. That's what I was trying to say.
çağdaş