c# - ASP.NET Core - 搜索栏结果在 View 中返回 null

标签 c# asp.net .net asp.net-core razor

我有一个基本的搜索框,我想在用户按下提交/单击回车键时向其显示数据。单击时显示 div,但数据返回 null,即使我可以单步执行数据访问代码并看到数据正在添加到列表中。谁能帮我解决这个问题?

我已经尝试了多种方法来完成这项工作。我相信我的问题出在 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 函数中,它用数据填充对象,当我在 Home Index View 中调试它时,它显示为 null。谁能帮忙?我相信我非常接近。

最佳答案

我更改了我的 DataAccess 函数,获取并取出对象的单个实例而不是列表。我还使 SearchViewModel 属性“查询”等于搜索字符串,因此它不会为空。

更新的 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 现在会在我搜索时显示适当的结果。

关于c# - ASP.NET Core - 搜索栏结果在 View 中返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57114989/

相关文章:

C# - 在线程中使用带有 "out"参数的函数

c# - 如何创建匹配的 HMAC 值来验证 .NET 中的 Shopify WebHook?

asp.net - ASP :image or img

c# - URL 的上次修改日期

c# - AccessViolationException 未处理

c# - 试图找到 EF6 的正确实现

c# - 枚举在 C# 中有成员限制吗?

.net - 使用 VS 2010 进行单元测试的教程

c# - 使用第二个 UpdatePanel 的 gridview 中的 LinkBut​​ton 从第一个 UpdatePanel 更新文本框。找不到控件

c# - 无法将 PLAINTEXT 签名与 DotNetOpenAuth ServiceProvider 一起使用