0

I've been trying to display a form using cshtml for quite some time and it's given me a lot of issues. I'm using ASP.NET MVC.

This is my Controller code:

    [HttpGet]
    public ActionResult Search()
    {
        return View();
    }

    [HttpPost]
    public ActionResult Search(Models.SearchModel user)
    {

        List<Models.SearchModel> UserList = new List<Models.SearchModel>();

        MySqlConnection connection = DBConnect.getconnection(); // setting connection to database
        MySqlCommand cmd = new MySqlCommand("GetUsers", connection); // search for procedure called "GetData"
        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add(new MySqlParameter("?search", MySqlDbType.VarChar)); // search parameters, if not looking for anythinf gets all the data
        cmd.Parameters["?search"].Value = "%" + "" + "%";
        cmd.Parameters["?search"].Direction = ParameterDirection.Input;

        MySqlDataReader dr = cmd.ExecuteReader(); // telling program to read Data
        while (dr.Read())
        {
            int id = Convert.ToInt16(dr["ID"]);
            string user_name = Convert.ToString(dr["user_name"]); // converting data to a string

            Models.SearchModel UserMod = new Models.SearchModel(id, user_name);

            UserList.Add(UserMod);

        }

        dr.Close(); // close

        DBConnect.CloseConnection(connection); // closes connection


        return View("Search");
    }

My Model:

 namespace AOSExpress.Models
 {
   public class SearchModel
 {
    private int id;
    public int Id
    {
        get { return id; }
        set { id = value; }
    }

    private string user_name;

    public string User_Name
    {
        get { return user_name; }
        set { user_name = value; }
    }

    public SearchModel(int i, string usnm)
    {
        id = i;
        user_name = usnm;

    }
}
}

and my Search.cshtml:

 @model IEnumerable<AOSExpress.Models.SearchModel>

 @{
 ViewBag.Title = "Search";
 Layout = "~/Views/Shared/_Layout.cshtml";
 }


 @foreach (var item in Model)
 {
   @Html.Partial("_SearchModel", item)
 }

and _Search.cshtml

  @model AOSExpress.Models.SearchModel

   <table style="font-family: Arial; border:1px solid black; width: 300px">
    <tr>
        <td><b>ID:</b></td>
        <td>@Model.Id</td>
    </tr>
    <tr>
        <td><b>Username:</b></td>
        <td>@Model.User_Name</td>
    </tr>

</table>

The error is:

An exception of type 'System.NullReferenceException' occurred in App_Web_5m4f2la2.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

John Saunders
  • 157,405
  • 24
  • 229
  • 388
DiegoAR
  • 13
  • 2
  • 7
  • Try not using Partial – Gandarez Oct 11 '13 at 20:51
  • 1
    you are passing a string "search" to your view while it is expecting a collection of search model – Vinay Pratap Singh Oct 11 '13 at 20:51
  • not really related to the problem, but are you sure you want a separate table for each result? or should they be rows of a larger table? – Mr.Mindor Oct 11 '13 at 22:15
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 13 '13 at 02:17

2 Answers2

3

It could be that you are setting the view to the wrong ActionResult. In other words if you set the view to the Search with no code and just the return View(); code, you will get a System.NullReferenceException because the model is not within that method. Also, If all you want to do is display a table and not require user input there really is no need for [HttpGet] and [HttpPost].

So to fix this you might want to take the following steps:

Cut all the code within the [HttpPost] Search and Paste it within the [HttpGet] Search.

Then completely erase the [HttpPost] Search method.

Then remove the [HttpGet] above the public ActionResult Search() along with the extra return View(); you might have after copying the code over.

Hope this helps.

Oh and make sure that you pass the model to the view return View(UserList);.

Code5
  • 46
  • 4
2

You are not passing your list of SearchModels to the view. So Model in the view is null.

return View("Search", UserList);

This should resolve your issue.

Additionally, ASP-MVC is highly convention based. Since your action and the view have the same name (Search) you could omit the first parameter from your call to View:

return View(UserList);
Mr.Mindor
  • 3,869
  • 2
  • 16
  • 25
  • Can you update your question with the full stack trace? If you debug this in Visual Studio, place a breakpoint near the beginning of Search() , does the exception occur before or after the breakpoint? – Mr.Mindor Oct 11 '13 at 22:15