3

I'm currenty manage to get some time to work on ASP.NET MVC. I'm doing the tutorial Create a Movie Database in ASP.NET MVC, which still uses the ADO.NET Enity Model. I managed to create a List View from the LINQ Entity Model. So here is my problem. The Bind Attribute doesn't work on my SQL Entity.

Original Code with Ado.NET

        public ActionResult Create([Bind(Exclude="Id")] Movie movieToCreate)

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

            _db.AddToMovieSet(movieToCreate);
            _db.SaveChanges(); 
            return RedirectToAction("Index");
        } 

My LINQ Code

    public ActionResult Create([Bind(Exclude = "Id")] Movies movieToCreate)
    {
        if (!ModelState.IsValid)
        {
            return View();
        }
        _db_linq.Movies.InsertOnSubmit(movieToCreate);
        _db_linq.SubmitChanges();

        return RedirectToAction("Index");
    }

But the Id Field isn't excluded. Any Ideas? Thanks!

2
  • Why do you think what it isn't excluded? Commented May 28, 2009 at 9:35
  • 1
    Because movieToCreate still contains the id field set to 0. but I solved that problem by declaring the id field as identiy and primary key in the db and autocreated = true in the shema. Commented May 28, 2009 at 10:00

1 Answer 1

7

Your ID property is probably an int and it's not a nullable type. And because of that, even though it's excluded when binding, it's got to have a value. In this case it has the default value of its type, which is zero.

Make sure you set up your database properly, having the ID field's IsIdentity property set to true and re-create your LINQ classes.

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

4 Comments

I have a situation about this which Exclude doesnt work when i declare it in the Action Level rather than a class level. if i declare it on the partial class of an entity it works. Do you have any idea what might be the reason
@Barbaros Alp, Just saw your question, (stackoverflow.com/questions/2289456). And I think Craig Stuntz's answer is correct.
Yes i have marked it as answered but why it works when i changed the order of the parameters ?
@Barbaros Alp, You didn't change the order of parameters there. You were just applying the Bind attribute to the wrong parameter.

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.