database - comparing a string value returned from ToArray() method in c# -
following code written me searching specific item name in advertisement table.
public actionresult searchresult(string name) { var advertisement = db.advertisements.toarray(); // retrieve data database foreach (var ad in advertisement) { if (ad.title.equals(name)) { return view(ad); } } return view(advertisement); }
even though search item in database, in cases if condition not being true.each time whole list of items result in view page. issue here?
my model advertisement looks this.
using system; using system.drawing; // image type in namespace using system.collections.generic; using system.linq; using system.web; using system.componentmodel.dataannotations; namespace bartering.models { public class advertisement { [key] public int id { get; set; } [required] [stringlength(100)] public string title { get; set; } public guid ownerid { get; set; } [required] public string category { get; set; } public byte[] image { get; set; } [required] [stringlength(200)] public string description { get; set; } } }
i think should doing this
public actionresult searchresult(string name) { var ad=db.advertisements.where(s=>s.title.toupper()==name.toupper()) .firstordefault(); if(ad!=null) return view(ad); //nothing found search name, let's return "notfound" view return view("notfound"); }
this code first item(if exists) matches our check (title==name) , return it. if there nothing found matches condition, return view called "notfound"
Comments
Post a Comment