views:

1681

answers:

1

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!

+4  A: 

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.

çağdaş
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
@Barbaros Alp, Just saw your question, (http://stackoverflow.com/questions/2289456/). And I think Craig Stuntz's answer is correct.
çağdaş
Yes i have marked it as answered but why it works when i changed the order of the parameters ?
Barbaros Alp
@Barbaros Alp, You didn't change the order of parameters there. You were just applying the `Bind` attribute to the wrong parameter.
çağdaş