Andrew Reese
Andrew Reese

Reputation: 912

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. 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. 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.

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 (Home View)

@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. Can anyone help? I believe I am very close.

Upvotes: 3

Views: 609

Answers (1)

Andrew Reese
Andrew Reese

Reputation: 912

I changed my DataAccess function grab and out a single instance of the object rather than a list. I also made the SearchViewModel property "Query" equal to search string so it will not be null.

Updated 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.

Upvotes: 3

Related Questions