c# - Search using Linq from the view -


i trying make textbox in view, can search using linq statements.

when try implement search view, compiler cant find prods list.

when try implement controller, cant find searchbox, in both case understand , dont understand how fix problem. or if best implement search in controller or in view ??

i have made comment in code, tried search function.

using system; using system.collections.generic; using system.linq; using system.web; using system.web.mvc; using uppgift_1.models;  namespace uppgift_1.controllers {     public class productcontroller : controller     {          public actionresult index()         {             list<myproduct> prods = new list<myproduct>()             {                 .....add products......             };  // linq search             var searchresults = (from s in prods where(s.productname == searchbox || s.productid == searchbox) select s).tolist();              return view(prods);         }      } }      @model ienumerable<uppgift_1.models.myproduct>  @{     layout = null; }  <!doctype html>  <html> <head>     <meta name="viewport" content="width=device-width" />     <title>index</title> </head> <body>          @foreach (var x in model)         {              <div>                 <h3>@x.productname</h3>                 article# @x.productid                 <h4>@x.pricesell.tostring("c")</h4>             </div><hr />                 }        <div>         <input type="text" name="searchbox" />               // link search             @{             var searchresults = (from s in prods (s.productname == searchbox ||     s.productid == searchbox)                                  select s).tolist();              }       </div> </body> </html> 

you passing prods list model view have not under same variable name. add line view under layout = null;:

var prods = model; 

mvc not web forms can see controls code behind , see code behind variables markup (aspx). there clear separation between controllers , views, have explicitly pass information between them. data passed controller view via model or more general viewbag , viewdata collections. data passed views controllers via request data can come several sources; route values, querystring or form values.

as perform search, views should contain view logic relevant assembling view. believe search functionality decidedly fall outside category should either implemented in controller or, according strict separation of concerns, implemented in repository/data access layer.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -