ASP.NET Core getting a value from an input box with a c# variable -
i working on project in there needs search box find records containing input string , display them onto page. ideally, requirement filter out results string being typed in, if that's impossible, search button find end results work. not know asp.net core, not know how in slightest bit. @ first, thought may able taking input value , putting sql statement, displaying it, like:
(context: using visual studios 2017 enterprise, in .cshtml file)
<input id="pleasework" type="text" /> <button onclick="@workalready()">search</button> @{ public void workalready(){ var inpstring = document.getelementbyid("pleasework").value string sql = "select * table (id = '" + inpstring + "')"; sqlconnection connect = new sqlconnection(connection); sqlcommand command = new sqlcommand(sql, connect); conn.open(); sqldatareader nwreader = command.executereader(); /*...display results*/ } }
this work okay - (okay being relative problem, disregarding bad coding practices , ugly code) - if able , input field value c# variable, both types of variables not playing each other, cannot find way of doing this. i've searched lot of places, , hear lot talking using controllers , like, limited knowledge of .net core (as mvc altogether,) cannot work. guess question wondering if possible way? can input value c# variable? appreciated!
i not recommend learning mvc framework, new language. you're trying right calling pre-processed method in processed page (hint: it's impossible). need define controllers , actions in server side codebase (i.e. controller layer, c in mvc).
you can use whatever language in frontend call controller (i recommend using jquery since you're new this). jquery javascript, , therefor can call functions define in jquery script click on input field.
so 2 things, learn how send ajax request (post) server. secondly, learn how create simple controller - action in codebase.
once have 2 set, can communicate between view , controller. after can start thinking sql, no sooner.
Comments
Post a Comment