1

From an Index page, is it possible to get the text value from an input like this:

<input id="inpSearchMembers" class="form-control small-text" placeholder="Search for a Member..." />

and feed it into the asp-route-searchText attribute value in something like this:

<a id="btnSearchMembers" class="btn btn-outline-primary small-text" asp-page="/Search" asp-route-searchText="~Value from InpSearchMember~" >Search</a>

So that the attribute asp-route-searchText contains the text of the input field id="inpSearchMembers" before sending back to the code-behind when clicked?

1 Answer 1

2

the asp-route-... will be used by the AnchorTagHelper to render the attribute href once and statically without any magic binding like what you wish for. So for example, if you have a route pattern like /abc/{searchText}. After being rendered to HTML, the A element will have its href rendered like /abc/whatever-from-asp-route-searchText. I would not try to send value via route data in such use cases if possible. But if you have to, you need to use javascript here to replace the searchText (initially rendered as a unique token inside the href) with the dynamic value from the other input before the link is visited by clicking.

First you prepare the code in the razor view like this:

@{
   var searchToken = Guid.NewGuid().ToString();
}
<a id="btnSearchMembers" class="btn btn-outline-primary small-text" 
   asp-page="/Search" asp-route-searchText="@searchToken">Search</a>

Next, you write code for the javascript part to replace the search token with runtime (dynamic) value from the other input:

window.onload = function(){
    var searchLink = document.getElementById("btnSearchMembers");
    var originalHref = searchLink.href;
    searchLink.onclick = function(e){
       //get the value from the other input and replace the link's href 
       //before it's visited by the link click.
       var searchTextElement = document.getElementById("inpSearchMembers");
       e.target.href = originalHref.replace("@searchToken", searchTextElement.value);  
    };
}

Of course the above javascript code should be written in the same razor view file so that you can access to the server variable searchToken.

Sign up to request clarification or add additional context in comments.

6 Comments

This is great, and makes sense so thanks, I have learned something today! I have since found the best way to do this is to use a form submit technique. This is great to see working though so thank you again!
@CJH you're welcome, yes using form is good however the point here is how you send the dynamic data, it should be directly from a form field/element instead of having to be put in a route value (forming the request path). By forming the request path, it's convenient only when that value is not taken from users or you have non-browser clients. For browser clients, we should use HTML forms.
Brill, that makes sense...! This is all very good to understand so thank you again!
@AndresElizondo that syntax is not supported. The AnchorTagHelper (as well as any other kinds of TagHelper) has properties predefined and their names are based on literal text at design time. If the razor page code can be dynamically generated, then you can write it that way. However you can use asp-all-route-data to specify a route data dictionary instead and it's completely dynamic, like this: asp-all-route-data="@(new Dictionary<string,string> { [searchRoute] = "someValue" })"
That's perfect!! Thank you @KingKing! Very well explained as well, I appreciate it!
|

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.