今天我一直在四处看看如何将我创建的这个字符串列表显示给名为“results”的操作结果。我想知道是否有人知道如何在.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();
}
我在许多不同的版本中尝试过“for every var”但没有成功。到目前为止这是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 :(得分: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);
现在在View
@model List<string>
@foreach (var result in Model)
{
<p>@Html.DisplayFor(m => result)</p>
}
答案 1 :(得分:0)
在ASP.Net MVC中,您需要通过&#34; model&#34;到&#34;查看&#34;这样您就可以从视图中访问信息。通常由具有模型参数的Controller.View
方法之一完成。
我不清楚你实际上想要展示什么,但是下面应该让你开始:
FlyoutsControl