简体   繁体   English

ASP.NET Core - 搜索栏结果在视图中返回 null

[英]ASP.NET Core - Search Bar results coming back null in View

I have a basic search box that I want to display data to when the users presses submit/clicks the enter key.我有一个基本的搜索框,我想在用户按下提交/单击 Enter 键时向其显示数据。 The div displays when clicked but the data is coming back null even though I can step through my Data Access code and see the data is being added to a list.单击时会显示 div,但即使我可以单步执行数据访问代码并查看数据正在添加到列表中,数据也会返回 null。 Can anyone help me with this problem?谁能帮我解决这个问题?

I have tried multiple things to make this work.我尝试了多种方法来完成这项工作。 I believe my problem is in the Homecontroller and how the View is being displayed.我相信我的问题在于 Homecontroller 以及 View 的显示方式。

SearchViewModel.cs搜索视图模型.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数据访问.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家庭控制器.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 (Home View) 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>
}

The searchString is being passed into the DataAccess Function and it is filling the object with the data, it is showing as null when I debug it in the Home Index view. searchString 被传递到 DataAccess 函数中,它正在用数据填充对象,当我在 Home Index 视图中调试它时它显示为 null。 Can anyone help?任何人都可以帮忙吗? I believe I am very close.我相信我非常接近。

I changed my DataAccess function grab and out a single instance of the object rather than a list.我更改了我的 DataAccess 函数抓取并取出对象的单个实例而不是列表。 I also made the SearchViewModel property "Query" equal to search string so it will not be null.我还使 SearchViewModel 属性“查询”等于搜索字符串,因此它不会为空。

Updated DataAccess.cs更新的 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;
            }
        }

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

My div now displays the appropriate results when I search.当我搜索时,我的 div 现在显示适当的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM