0

I am getting a runtime error with my application when I try to go from one View to another;

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Which points to 'Countries' in the following code of the View I am trying to access;

@model Customer
...
<div class="form-group">
    <label asp-for="Country">Country</label>
    <select asp-for="Country" class="form-control">
        <option value="">Select a country...</option>
        @foreach (Country c in ViewBag.Countries) //Error
        {
            <option value="@c.CountryID">@c.Name</option>
        }
    </select>
</div>
....

I am also getting an error when initializing and updating the database and I'm uncertain if they're related;

Error Number:2714,State:6,Class:16
There is already an object named 'Countries' in the database.

Let me know if there is any other code sample I can provide to clarify.

Alex
  • 23
  • 6
  • Where are you setting `ViewBag.Countries`? – gunr2171 Dec 11 '21 at 23:56
  • You have to prepare the new question where show all your views and actions. And don' t mention null reference in a Title again. Otherwise your question will be closed again. – Serge Dec 12 '21 at 00:06

0 Answers0