Reputation: 15
I've been looking around for hours today on how to display this list of strings I've created to an action result called "results". I was wondering if anyone knew how to display it on the .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 from Testimonial where Content like '%@p1%';"; //insert statement
SqlConnection db = new SqlConnection(@"");
SqlCommand command = new SqlCommand(query, db);
command.Parameters.AddWithValue("@p1", keyword); // seting @p1 to the content
db.Open();
SqlDataReader reader = command.ExecuteReader();
DataTable results = new DataTable();
results.Load(reader); //Loads remaining surgeon credentials into a 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 to the 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 with no success. This is the aspx so 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>
Upvotes: 0
Views: 1388
Reputation: 55248
Make these changes in your 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 the controller
var results = new Testimonials().getSearchResults("blah");
return View("Testimonials", results);
Now at View
@model List<string>
@foreach (var result in Model)
{
<p>@Html.DisplayFor(m => result)</p>
}
Upvotes: 1
Reputation: 100545
In ASP.Net MVC you need to pass "model" to "view" so you can access information from the view. Usually it is done by one of Controller.View
methods that have model parameter.
It is not exactly clear to me what you are actually trying to display, but following should get you started:
public ActionResult Testimonials()
{
getSearchResults("test");
return View(result);
}
Upvotes: 0