-1

Im trying to insert data into a table on my main view from my HomeController and it is returning a null exception and I cant figure out why.

My controller:

 public ActionResult FillTable()
    {
        var MyState = new State();

        var StateValues1 = new State();
        StateValues1.DayTableDates = "monday";
        StateValues1.DayTableValues = "15";
        MyState.TableValuess.Add(StateValues1);

        return View(MyState);
    }

My view:

 @{
        var StateValues1 = Model.TableValuess.FirstOrDefault(); 
    }

    <table class="greyGridTable">

        <thead>
            <tr>
                <th>Date</th>
                <th>Value</th>

            </tr>

        </thead>

        <tr>
            <td>@StateValues1.DayTableDates</td>
            <td>@StateValues1.DayTableValues</td>
        </tr>

    </table>

my Model:

 public class State
{

    public List<State> TableValuess = new List<State>();

    public string DayTableDates { get; set; }
    public string DayTableValues { get; set; }


}

null exception my index method: Index method Im fairly new to MVC but in my head this should work. Excuse the variable names, ive been messing with it for quite a while now. Thanks for the help
edit: I think the error has something to do with my view being called Index while my action method in the HomeController is called FillTable(). My index method in my controller already has code in it. How would I go about calling my FillTable method or adding my code to the Index method?

  • Ive tried to debug the NullReferenceException but I cant because I dont understand where it is getting the null from. I thought I was assigning values when I made StateValues1 – Joshua Weiss Sep 27 '18 at 13:13
  • 1
    Where is the exception thrown? Did you log it? Post the *full* exception message, including the call stack. You can get it easily with Exception.ToString(). The call stack will show all the methods involved up to the point the exception was thrown. – Panagiotis Kanavos Sep 27 '18 at 13:25
  • BTW `StateValues1 = Model.TableValuess.FirstOrDefault()` will return *null* if the list is empty. Your controller never added an entries to it. I'd bet that's why `@StateValues1.DayTableDates` throws – Panagiotis Kanavos Sep 27 '18 at 13:26
  • 1
    Arent I adding to the list on the line MyState.TableValuess.Add(StateValues1); ? I added a picture to the post to show where the error occurs – Joshua Weiss Sep 27 '18 at 13:33

3 Answers3

1

From the debug window it looks like you opened /Home/Index, which calls action method Index() in your HomeController.

However, the action method that fills the object is called FillTable() and to run that method you should be calling the URL /Home/FillTable.

Note that if that is what you want, then you will also need to make sure that either you have a corresponding View called FillTable.cshtml, or you make FillTable() invoke the default view using View("Index", MyState).

OR...

You could also move all the code from the FillTable() action method into your Index() action method.

OR...

Change FillTable() to end with return MyState; (also change the method return type). Then do var MyState = FillTable(); in the Index() method, so you can use MyState there. Then you can just keep using /Home/Index.

Peter B
  • 22,460
  • 5
  • 32
  • 69
  • This looks like it could be the answer! This table is a small part of a bigger project so I cant make a new view just for it. I already have code in my index class. I tried adding return View("Index", MyState) to the FillTable() action method but got the same null error. Can I just add the FillTable() code to the existing code in my index? – Joshua Weiss Sep 27 '18 at 13:53
  • I edited my post with my updated problem. I think it does have something to do with the index method – Joshua Weiss Sep 27 '18 at 14:01
  • Please see updated answer. – Peter B Sep 27 '18 at 14:12
  • I changed FillTable() to pubflic State FillTable() and returned MyState. I then added var MyState = FillTable(); to Index() but same null error. Is it my method return type or maybe the fact that Index() takes a parameter? – Joshua Weiss Sep 27 '18 at 14:19
0

on you View you must specify the Model you will use. Try like:

@model myProjectnamespace.Models.State

<table class="greyGridTable">
        <thead>
            <tr>
                <th>Date</th>
                <th>Value</th>   
            </tr>   
        </thead>    
        <tr>
            <td>Model.DayTableDates</td>
            <td>Model.DayTableValues</td>
        </tr>
</table>
apomene
  • 14,282
  • 9
  • 46
  • 72
  • Hey sorry I forgot to include it but I do have @model InternetOfWaterFinal.Models.State. I tried changing my table code to Model.DayTableDates and it now shows "". – Joshua Weiss Sep 27 '18 at 13:42
0

Issue seems to be state class, please try this :

public class State
{
    public State()
    {
          TableValuess = new List<State>();
    }
    public List<State> TableValuess { get; set; }

    public string DayTableDates { get; set; }
    public string DayTableValues { get; set; }    

}
Pranav Singh
  • 17,079
  • 30
  • 77
  • 104