1

So I am very new to MVC and development. What I am trying to do is basically create a single row table view of an user so that I can edit and update on that single row. (I hope I am making sense)

So I am using MVC Razor View and I want to create a table and edit within that table without using the CRUD functionality that is already built into MVC. I want to do inline editing of my tabular view.

So I have create a view that also has a partial view. I have a Model and my Repository as well. WHen I run this, it says Object not set to an instance of an object, and it's talking about my foreach loop. Here is the data below. If someone can offer assistance, that would be greatly appreciated. I am very new and only been really coding for about 3 months now.

Controller

public ActionResult Index(string id)
        {
            var attendee = (Guid)Membership.GetUser(id).ProviderUserKey;
            CatalogAttendeeModel model = new CatalogAttendeeModel();
            model.getAttendeesList(attendee);
            var catalog = from ca in db.text
                          join a in db.text on ca.textID equals   a.textID
                          where ca.textID== attendee
                          select ca;
            foreach (var item in catalog)
            {
                if (item.IsActive == null)
                {
                    item.IsActive = true;
                }
            }
            return PartialView(); 
        }

Index View

@model ODT4.Models.CatalogAttendeeModel

<table>
    <thead>
        <tr>
            <th>
                @Html.Label("Catalog")
                @Html.Label("Role")
                @Html.Label("Is Admin")
                @Html.Label("Certificate Printed")
                @Html.Label("Percent Watched")
                @Html.Label("Percent Updated")
                @Html.Label("PI Name")
                @Html.Label("Action")
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (var row in Model.catalogAttendees) //this is where I get the errormessage
        {
            {
                Html.RenderPartial("CatalogAttendeeSingleRow", row);
            }
        }
    </tbody>
</table>

Partial View

@model ODT4.Models.CatalogAttendeeModel

@{
    <div id = "crud_tableEdit" >
        <table>
            <tr>
                <td>@Html.DisplayFor(m => m.Catalog.CatalogCode)</td>
                <td>@Html.DisplayFor(m => m.StudyRole)</td>
                <td>@Html.DisplayFor(m => m.IsAdmin)</td>
                <td>@Html.DisplayFor(m => m.IsTrainingDocPrinted)</td>
                <td>@Html.DisplayFor(m => m.PercentWatched)</td>
                <td>@Html.DisplayFor(m => m.PercentUpdatedOn)</td>
                <td>@Html.DisplayFor(m => m.PIName)</td>
            </tr>
        </table> 

    </div>

}

CatalogModel

 public List<Catalog_Attendee> catalogAttendees { get; set; }

    public CatalogAttendeeModel()
    {

    }
    public void getAttendeesList(Guid id)
    {
        //catalogAttendees = new List<Catalog_Attendee>();
        CatalogAttendeeRepository rep = new CatalogAttendeeRepository();
        catalogAttendees = rep.getAttendeesAll();
    }

Repository

public List<Catalog_Attendee> getAttendeesAll()
        {
            return db.Catalog_Attendee.ToList();
        }

        public Catalog_Attendee getAttendeeByID(Guid id)
        {
            return db.Catalog_Attendee.FirstOrDefault(i => i.AttendeeID == id);

        }

Here is the error message:

Server Error in '/' Application.
________________________________________
Object reference not set to an instance of an object. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

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

Source Error: 

Line 17:     </thead>
Line 18:     <tbody>
Line 19:         @foreach (var row in Model.catalogAttendees)
Line 20:         {
Line 21:             {
Brian Tompsett - 汤莱恩
  • 5,195
  • 62
  • 50
  • 120
Carla
  • 111
  • 1
  • 7
  • The 'linq' query is not processed until you actually consume it in the 'for' loop. Are you sure that the 'db.text', or sub properties, in the query are not null? – karmasponge Mar 04 '15 at 22:12
  • I just returned the model in my Partial View and now the error is gone. -) – Carla Mar 05 '15 at 13:57

1 Answers1

1

Model.catalogAttendees is null - hence the exception.

As to why ... maybe the linq expression is not returning what it should. I'm not seeing what db.text should be, but then again I'm not seeing all the code.

Edit: Also, you're not returning the model to the view. return PartialView(model);

Mackan
  • 6,031
  • 2
  • 23
  • 44
  • db.text is my database. I just removed the name of the table and inserted text just to leave out the actual table name, I guess that was silly. I just added return PartialView(model), and it runs with no errors. Thank you Mackan – Carla Mar 05 '15 at 13:56
  • Glad to hear it. Please mark answer as accepted to close the question. – Mackan Mar 05 '15 at 14:46