2015-04-26 56 views
0

今天我一直在四處尋找幾個小時,如何顯示這個我創建的字符串列表到稱爲「結果」的操作結果。我想知道是否有人知道如何在.aspx頁面上顯示它?顯示搜索結果

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(); 
    } 

我試過「對於每個變種」在很多不同的變化沒有成功。這是迄今爲止的aspx:

<%@ 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> 
+0

我編輯了自己的冠軍。請參閱:「[應該在其標題中包含」標籤「](http://meta.stackexchange.com/questions/19190/)」,其中的共識是「不,他們不應該」。 –

回答

1

請在您的代碼這些變化。

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; 
} 
現在

控制器

var results = new Testimonials().getSearchResults("blah"); 
return View("Testimonials", results); 

現在在裏面查看

@model List<string> 
@foreach (var result in Model) 
{ 
    <p>@Html.DisplayFor(m => result)</p> 
} 
0

在ASP.Net MVC中,您需要將「model」傳遞給「view」,以便您可以從視圖中訪問信息。通常它是由具有模型參數的Controller.View方法之一完成的。

這是不完全清楚,我什麼你實際上是試圖顯示,但下面應該讓你開始:

public ActionResult Testimonials() 
{ 
    getSearchResults("test"); 
    return View(result); 
}