I am using `@Html.Textbox("searchString") in Razor view page. I need this value of textbox in
@Html.ActionLink("View Items", "Index", new { id = item.transaction_id, searchString = "Value of textbox"}).
Of course the search string part in html action link is not working right now but i need this as i have specific route which works according to search string.
How do i pass value of textbox to action link?
i check this this,this,this and this.
What i tried is
<script type = "text/javascript">
var value = document.getElementbyID("searchString").Text;
var link = @Html.ActionLink("View Items", "Index", new { id = item.transaction_id, searchString = -1});
link.replace(-1,value);
</script>
Still no luck. I understand Razor renders at server side.
UPDATE
i have following textbox on the top of view:
@using (Html.BeginForm("Index", "trans_junction", FormMethod.Get))
{
<p>
Request No: @Html.TextBox("searchString")
<span><input type="submit" value="Search Request" /></span>
</p>
}
This textbox is search box in which user can search items.
there is an Action link as follows:
@Html.ActionLink("View Items", "Index", new { id = item.transaction_id }) |
and a route config:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}/{searchString}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, searchString= UrlParameter.Optional }
);
}
}
Now i need to pass the values as my route map via actionlink. As suggested in answer by StephenMuecke, i tried to modify my @using (Html.BeginForm("Index", "trans_junction", FormMethod.Get))
with @using (Html.BeginForm("Index", "trans_junction", new { id = item.transaction_id }, FormMethod.Get))
but item is not accessible there.
When user click search button, url is http://localhost:57286/trans_junction/Index?searchString=20
if user click on Action link url is http://localhost:57286/trans_junction/Index/88
but i actually need is http://localhost:57286/trans_junction/Index/88/20
which preserve the the result of search and also pass the id.
I am adding screenshots for better understanding.
This is index view without search.
This is search result with searchString = 10.
This is after clicking the action link i.e. View Items, Here search results are not preserved.