c# - Displaying search results -
i've been looking around hours today on how display list of strings i've created action result called "results". wondering if knew how display on .aspx page?
public class testimonials { public string addtestimonials { get; set; } public string searchtestimonials { get; set; } public list<string> results = new list<string>(); public void getsearchresults(string keyword) { string query = "select content testimonial content '%@p1%';"; //insert statement sqlconnection db = new sqlconnection(@""); sqlcommand command = new sqlcommand(query, db); command.parameters.addwithvalue("@p1", keyword); // seting @p1 content db.open(); sqldatareader reader = command.executereader(); datatable results = new datatable(); results.load(reader); //loads remaining surgeon credentials data table. foreach (datarow row in results.rows) { string cont = row["content"].tostring(); this.results.add(cont); } db.close(); } } public actionresult testimonials() { return view(); } [httppost] [authorize] public actionresult testimonials(testimonials model, string returnurl) { if (model.addtestimonials != null) { string query = "insert testimonial (content,date,surgeonid) values (@p1,current_timestamp,@p2);"; //insert statement sqlconnection db = new sqlconnection(@""); sqlcommand command = new sqlcommand(query, db); command.parameters.addwithvalue("@p1", model.addtestimonials); // seting @p1 content command.parameters.addwithvalue("@p2", convert.tostring(session["surgeonid"])); db.open(); command.executenonquery(); db.close(); return redirecttoaction("testimonials"); } if (model.searchtestimonials != null) { model.getsearchresults(model.searchtestimonials); return redirecttoaction("testimonials"); } return view(); }
i've tried "for each var" in many different variations no success. aspx far:
<%@ page title="" language="c#" masterpagefile="~/views/shared /site.master"inherits="system.web.mvc.viewpage<team3oie2s.models.testimonials >"%> <asp:content id="content1" contentplaceholderid="titlecontent" runat="server"> testimonials </asp:content> <asp:content id="content2" contentplaceholderid="maincontent" runat="server"> <form id="form1" runat="server"> <h2>testimonials</h2> <% using (html.beginform()) { %> <%: html.textboxfor(m => m.addtestimonials)%> <input type="submit" value="add" /> <% } %> <% using (html.beginform()) { %> <%: html.textboxfor(m => m.searchtestimonials)%> <input type="submit" value="search" /> <% } %> </form> </asp:content>
make these changes in code.
public list<string> getsearchresults(string keyword) { public list<string> results = new list<string>(); //.... foreach (datarow row in results.rows) { string cont = row["content"].tostring(); this.results.add(cont); } db.close(); return results; }
now inside controller
var results = new testimonials().getsearchresults("blah"); return view("testimonials", results);
now @ view
@model list<string> @foreach (var result in model) { <p>@html.displayfor(m => result)</p> }
Comments
Post a Comment