Reputation: 287
I have this HTML code in my cshtml:
<form method="get" action="@Url.Action("Index")">
@(Html.TextBox("q", Model.Search.FreeSearch))
<input type="submit" value="Search"/>
</form>
How Can I put in this html textbox default text which will return to my controller.
For example, in begining this textbox is empty and it must return to contrller empty value, But soon as I type something, e.g. JADA it must return to controller this: destination:"JADA" (destination will be default, it means that this text box will return only values that is in destination column, so much for info).
my route:
using System.Web.Mvc;
using System.Web.Routing;
namespace AdminRole
{
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
}
Controller that acepts value from Search:
public ISolrQuery BuildQuery(SearchParameters parameters)
{
if (!string.IsNullOrEmpty(parameters.FreeSearch))
return new SolrQuery(parameters.FreeSearch);
return SolrQuery.All;
}
public SortOrder[] GetSelectedSort(SearchParameters parameters)
{
return new[] { SortOrder.Parse(parameters.Sort) }.Where(o => o != null).ToArray();
}
Realy thanks for help.
Upvotes: 0
Views: 361
Reputation: 156978
Well, actually the way you already did by supplying FreeSearch
:
@Html.TextBox("q", !string.IsNullOrEmpty(Model.Search.FreeSearch)
? Model.Search.FreeSearch
: "Default"
, new { placeholder = "Placeholder text" }
)
The last line adds the html attribute placeholder
to show the text in the background. This text is not submitted to the server.
Changing the route:
routes.MapRoute(
name: "Search",
url: "Home/Search/{q}",
defaults: new { controller = "Home", action = "Search", q = "Default value" }
);
Upvotes: 1
Reputation: 62488
yo can do like this:
if(!String.IsNullOrWhiteSpace(Model.Search.FreeSearch))
@(Html.TextBox("q", Model.Search.FreeSearch))
else
@(Html.TextBox("q", Model.Search.FreeSearch, new { Value="Default Text Here"}))
Upvotes: 1