0

So I'm trying to do an update to an Entity model but it's not going as well as I'd like. I'm only trying to update a few fields in my model, but when I have the following code run, I get a SqlException complaining that there are fields (that I'm not trying to edit) that do not allow nulls. It's seeming like it's trying to create a new row in the database, instead of just updating the existing one? Not too sure what to make of it. Any help would be appreciated.

[HttpPost]
public ActionResult Budget(Proposal proposal)
{
    if (ModelState.IsValid)
    {
        db.Proposals.Attach(proposal);
        db.ObjectStateManager.ChangeObjectState(proposal, EntityState.Modified);
        db.SaveChanges();
        return RedirectToAction("Details", new {id = proposal.ProposalID});
    }

    return View(proposal);
}

Here's the SqlException that I'm getting: "System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Ulid', table 'casurg2_dev2.dbo.Proposals'; column does not allow nulls. UPDATE fails. The statement has been terminated."

And here's the view in question: http://pastie.org/7984222

6
  • Please post the full SqlException details. If it's trying to create a new record it's probably because you're expecting proposal to contain values - but the model binding may not have worked. Post the value of the proposal at this point. Commented May 30, 2013 at 15:18
  • What is Ulid to the Proposal Entity? Is it the primary key? a non-nullable foreign key? Still sounds like a model binding issue. If you put a breakpoint in this method - what are the values of the fields in question? You could post the Proposal Entity Class code - that may help too. Commented May 30, 2013 at 15:28
  • Updated the question with the exception. Commented May 30, 2013 at 15:28
  • Ulid is just a plain field, and it does not allow nulls. proposal in the code has everything at null, except the correct ID and any data I put in the right fields. Commented May 30, 2013 at 15:31
  • everything at null... that's not good ;) Time to post the view code in question; see if we can spot any model binding issues Commented May 30, 2013 at 15:36

1 Answer 1

1

Ok, the source didn't really help as much (and btw - this is SO, you can paste your code here; no need to be pasting it elsewhere ;)

I did ask for the class definition of the Proposal entity aswell... but in it's absence i can only suggest [at a guess] that your put the original value of Ulid as a hidden field alongside your ProposalId like such:

@Html.HiddenFor(model => model.ProposalID)  
@Html.HiddenFor(model => model.Ulid)

so that the model binder can wire it up correctly on the post.

There are 2 ways i can think of to get around the effort with all the hiddenfor's

  • Use the Add Controller Wizard to set the template to EntityFramework - then modify the related View code. throw away what you don't want etc.

enter image description here

  • Change the Action method to re-fetch the Proposal record (using its ProposalId) and then bind the changed data to it. This is not the kinda advice i should be giving (an extra, unnecessary round trip to the db)

      [HttpPost]
      public ActionResult Budget(Proposal proposal)
      {
          if (ModelState.IsValid)
          {
              var proposalToMerge = db.Proposals.Find(proposal.ProposalId);
              UpdateModel(proposalToMerge);
              // don't need this anymore as entity instance is being tracked
              // db.Proposals.Attach(proposalToMerge );
              // db.ObjectStateManager.ChangeObjectState(proposalToMerge , EntityState.Modified);
              db.SaveChanges();
              return RedirectToAction("Details", new {id = proposal.ProposalID});
          }
    
          return View(proposal);
      }
    

Or summing like that. Untested.

Sign up to request clarification or add additional context in comments.

2 Comments

Here's the Proposal model, it's coming from a EDMX. i.imgur.com/4L0im2n.png Obviously, there are a ton of fields, is there a way that I can make this work without having to have a ton of HiddenFor's all over the place?
Sweet deal, I did the second part to change around the controller method. Thanks for all the help :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.