I recently came across the following code which generates a QueryString URL which I can't understand, since the generated URL is " http://localhost/?q=samsung " Where "q" is the name of the parameter, which by testing is the name of the TextBox. My question is: How does Razor know that it should take the name of the TextBox to generate the QueryString using the name of said TextBox, is there a convention or something similar? Thank you very much in advance
<form method="get" action="@Url.Action("Index")"> @Html.TextBox("q", Model.Search.FreeSearch) <input type="submit" value="Search" /> </form>
This is what is called Data Binding (Model Binding or Data Binding) in Asp MVC. Here is a reference for Asp.Net Core, but it applies to all MVC ( https://docs.microsoft.com/es-es/aspnet/core/mvc/models/model-binding?view=aspnetcore-2.2 )
Basically what it's all about is that for each input html element in your form, it will be transferred, so to speak, to some property. If you post your form, it will be to a C# property, if you do a get then it will be on the query string, etc.
In your case, the input q is being bound to an element in your query string with the name q and the value samsumg.