0

I get this error when I invoke the Edit Action of one of my controllers.

Here is the C# code of the Edit action method

   [AcceptVerbs(HttpVerbs.Post)]
    public ActionResult Edit(cedetails detailToEdit)
    {
        validateDetail(detailToEdit);
        if (!ModelState.IsValid)
            return View();

        try
        {
            var originaldetail = (from d in entity1.cedetails
                                  where d.detail_id == detailToEdit.detail_id
                                  select d).FirstOrDefault();
            entity1.ApplyPropertyChanges(originaldetail.EntityKey.EntitySetName, detailToEdit);
            entity1.SaveChanges();
            return RedirectToAction("Index");
        }
        catch
        {
            return View();
        }
    }

This is the validateDetail Method:

protected void validateDetail(cedetails detailToValidate)
{

    if (detailToValidate.c_name.Trim().Length == 0)
        ModelState.AddModelError("c_name", "C name is required.");
        ModelState.SetModelValue("c_name", ValueProvider["c_name"]);
    if (detailToValidate.a_server.Trim().Length == 0)
        ModelState.AddModelError("a_server", "A server is required.");
        ModelState.SetModelValue("a_server", ValueProvider["a_server"]);
    if (detailToValidate.d_server.Trim().Length == 0)
        ModelState.AddModelError("d_server", "D server is required.");
        ModelState.SetModelValue("d_server", ValueProvider["d_server"]);
    if (detailToValidate.l_server.Trim().Length == 0)
        ModelState.AddModelError("l_server", "L server is required.");
        ModelState.SetModelValue("l_server", ValueProvider["l_server"]);
    if (detailToValidate.url.Trim().Length == 0)
        ModelState.AddModelError("url", "URL is required.");
        ModelState.SetModelValue("url", ValueProvider["url"]);
    if (detailToValidate.s_id.Trim().Length == 0)
        ModelState.AddModelError("s_id", "S ID is required.");
        ModelState.SetModelValue("s_id", ValueProvider["s_id"]);
}

I get the error in this line:

<%= Html.TextBox("c_name", Model.c_name) %>  

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

I have searched for this error and happened upon several solutions, but none of them worked for me. Please let me know if this can be resolved at all. I also will add that I have chosen to hide certain table columns in the view, including detail_id, by not just displaying them.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Animesh
  • 4,926
  • 14
  • 68
  • 110
  • 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 May 15 '14 at 19:23

2 Answers2

2

does your Index action set the Model? the edit action doesn't anywhere, so if the Model isn't set in the Index action (ala return View(cedetials)) then the Model will be null.

ChrisPelatari
  • 689
  • 5
  • 7
0

i Think blue_fenix has apoint in his answer. it seems that you are not setting the Model Here:

if (!ModelState.IsValid)
            return View();

And Here:

catch
        {
            return View();
        }

You need to return the Model, because the HTML Textbox Helper is spectating a Model that can´t be null. On each case, try returning the same binded Model:

return View(detailToEdit);
JOBG
  • 4,544
  • 4
  • 26
  • 47
  • Hi, I now understand what blue_fenix meant. I have changed the action method to return the model in each return statement and now I dont get the error, but the page posts to itself instead of redirecting to Index and the change is not reflected in the database. When I debug the Edit Method and 'step into' I noticed that entity1.ApplyPropertyChanges(originaldetail.EntityKey.EntitySetName, detailToEdit); this statement is not being stepped into and it directly goes to the catch statement which gives the same exception : Object reference not set to an instance of an object – Animesh Dec 11 '09 at 14:03
  • Its hard to tell whats going wrong, because i don't know what ApplyPropertyChanges is doing, it seems like on of those two parameters are null(most likelly originaldetail). One thing i don't understand is why you send "originaldetail.EntityKey.EntitySetName" instead of just "originaldetail" which seem to be the same type as "detailToEdit". – JOBG Dec 11 '09 at 14:30
  • ApplyPropertyChanges method takes entitySetName and the object changed as its parameters. I have tried it like you have said, but this method has no other overloads. I am actually following this way from ASP.NET MVC contact manager sample given in the website. – Animesh Dec 11 '09 at 15:07