tags:

views:

24

answers:

0

I'm using DataAnnotation for validation.

The Post for Create Action returns Model accountElement, and one of the field is AccountID which is required and is Validated using Dataannotation.

In the Post Action for create I'm hard wiring the AccountID value to 1 (accountElement.AccountID = 1; code below) and then I use TryUpdateModel. Shouldn't this Update the AccountID to 1.

But ModelState.IsValid is always false.

How do i update the Model value in the code before it gets Validated or is this facility added in ASP.NET MVC 2. Are there any work arounds??

01.[AcceptVerbs(HttpVerbs.Post)]   
02.        public ActionResult Create([Bind(Exclude = "AccountElementID")]AccountElement accountElement)   
03.        {   
04.            accountElement.AccountID = 1;   
05.  
06.            bool bol = TryUpdateModel(accountElement, new[] { "AccountID" });   
07.            if (ModelState.IsValid)   
08.            {   
09.                LoadUserControls();   
10.                return View(accountElement);   
11.            }   
12.            else  
13.            {   
14.                LoadUserControls();   
15.                return View(accountElement);   
16.            }   
17.        }