2015-04-26 215 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); 
}