我有一个基本的搜索框,我想在用户按下“提交” /单击Enter键时显示数据。单击时会显示div,但是即使我可以逐步浏览数据访问代码并看到数据已添加到列表中,数据也将返回空值。谁能帮助我解决这个问题?
我已经尝试了多种方法来完成这项工作。我相信我的问题出在Homecontroller中以及如何显示View。
SearchViewModel.cs
public class SearchViewModel
{
[DisplayName("Search Query *")]
[Required]
public string Query { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
DataAccess.cs
public static bool GetSearchData(string searchString, out List<SearchViewModel> lstModel)
{
lstModel = new List<SearchViewModel>();
try
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = connection.CreateCommand();
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "select Subject, Body from TABLE where Subject = @subject ";
command.Parameters.AddWithValue("@subject", searchString);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
SearchViewModel model = new SearchViewModel();
model.Subject = reader.GetValue(0).ToString();
model.Body = reader.GetValue(1).ToString();
lstModel.Add(model);
}
connection.Close();
return true;
}
catch (Exception exc)
{
exc.ToString();
return false;
}
}
HomeController.cs
List<SearchViewModel> lstModel = new List<SearchViewModel>();
public ActionResult Index(SearchViewModel model)
{
if (!ModelState.IsValid)
{
// There was a validation error => redisplay the view so
// that the user can fix it
return View(model);
}
else
{
string searchString = model.Query;
DataAccess.GetSearchData(searchString, out lstModel);
return View(model);
}
}
Index.cs(主视图)
@using (Html.BeginForm())
{
@Html.LabelFor(x => Model.Query)
@Html.EditorFor(x => x.Query)
@Html.ValidationMessageFor(x => x.Query)
<button type="submit">Search</button>
}
@if (Model.Query != null)
{
<div class="results-panel" style="display:block;" )">
<h4 class="card-title">@Model.Subject</h4>
@Model.Body
</div>
}
searchString被传递到DataAccess Function中,并用数据填充对象,当我在Home Index视图中对其进行调试时,它显示为null。有人可以帮忙吗?我相信我非常亲密。
答案 0 :(得分:2)
我更改了DataAccess函数的获取,并取出了对象的单个实例而不是列表。我还使SearchViewModel属性“ Query”等于搜索字符串,因此它不会为null。
更新的DataAccess.cs
public static bool GetSearchData(string searchString, out SearchViewModel searchModel)
{
searchModel = new SearchViewModel();
try
{
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();
SqlCommand command = connection.CreateCommand();
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "select Subject, Body from TABLE where Subject = @subject ";
command.Parameters.AddWithValue("@subject", searchString);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
searchModel = new SearchViewModel();
searchModel.Subject = reader.GetValue(0).ToString();
searchModel.Body = reader.GetValue(1).ToString();
searchModel.Query = searchString;
}
connection.Close();
return true;
}
catch (Exception exc)
{
exc.ToString();
return false;
}
}
更新后的HomeController
SearchViewModel searchModel = new SearchViewModel();
public ActionResult Index(SearchViewModel model)
{
if (!ModelState.IsValid)
{
return View(model);
}
else
{
string searchString = model.Query;
DataAccess.GetSearchData(searchString, out searchModel);
return View(searchModel);
}
}
搜索时,我的div现在显示适当的结果。