0

Currently on the model class level I have excluded from the model binder any attributes that should not be defined by users, such as CreatedDate or any navigation properties objects.

So inside my model class I define the following BIND list. This will not allow a user (or even a hacker) to include a value for the CreatedDate field, and this value will only be set inside the system code, when the object is created. So I did not include it in the Bind list as follow:-

[MetadataType(typeof(Group_validation))]
    [Bind(Include="Description,GroupID,Name")]
    public partial class Group
    {

But the problem is that inside the POST Edit action method, I have the following code:-

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Edit(Group group)
        {

            try
            {
                if (ModelState.IsValid)
                {
                    groupRepository.InsertOrUpdate(group);
                    groupRepository.Save();
                    return RedirectToAction("Index");
                }
            }
catch (DbUpdateConcurrencyException ex)
            {
                var entry = ex.Entries.Single();

                var clientValues = (Group)entry.Entity;

And this will raise an error since CreateDate field is required, and I am excluding it from the model binder? So how I can exclude the CreateDate from the model binder, but at the same time to avoid the null exception?

I know that I can just pass the objectID to the Edit action method, but this will prevent the DbUpdateConcurrencyException from being fired. so I had to pass the Group object to the edit action method , as I am doing in the above code.

2
  • May be CreateDate isn't DbNull. Did you check this?
    – Fals
    Jul 12, 2013 at 16:27
  • yes CreateDate is not null on the database,, and it should be like this.
    – John John
    Jul 13, 2013 at 9:24

0

Your Answer

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

Browse other questions tagged or ask your own question.