TryUpdateModel not working in ASP.NET MVC 1http://forums.asp.net/t/1502421.aspx/1?TryUpdateModel+not+working+in+ASP+NET+MVC+1Fri, 11 Dec 2009 06:19:43 -050015024213557370http://forums.asp.net/p/1502421/3557370.aspx/1?TryUpdateModel+not+working+in+ASP+NET+MVC+1TryUpdateModel not working in ASP.NET MVC 1 <p>I'm using DataAnnotation for validation.&nbsp;</p> <p>The Post for Create Action returns Model <strong>accountElement, </strong>and one of the field is <strong>AccountID </strong>which is required and is Validated using Dataannotation.</p> <p>In the Post Action for create I'm hard wiring the AccountID value to 1&nbsp;(<strong>accountElement.AccountID = 1; </strong>code below) and then I use TryUpdateModel. Shouldn't this Update the AccountID to 1.&nbsp;</p> <p>But ModelState.IsValid is always false.</p> <p><strong>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??</strong><br> </p> <pre class="prettyprint">[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude = &quot;AccountElementID&quot;)]AccountElement accountElement) { accountElement.AccountID = 1; bool bol = TryUpdateModel(accountElement, new[] { &quot;AccountID&quot; }); if (ModelState.IsValid) { LoadUserControls(); return View(accountElement); } else { LoadUserControls(); return View(accountElement); } }</pre> <p><br> &nbsp;</p> 2009-12-10T02:34:56-05:003557696http://forums.asp.net/p/1502421/3557696.aspx/1?Re+TryUpdateModel+not+working+in+ASP+NET+MVC+1Re: TryUpdateModel not working in ASP.NET MVC 1 <p>Support for DataAnnotations in new in MVC 2.<br> </p> 2009-12-10T06:32:55-05:003557707http://forums.asp.net/p/1502421/3557707.aspx/1?Re+TryUpdateModel+not+working+in+ASP+NET+MVC+1Re: TryUpdateModel not working in ASP.NET MVC 1 <p>&nbsp;how would i achive this in&nbsp;ASP.NET MVC 1. any work around????</p> 2009-12-10T06:42:02-05:003557795http://forums.asp.net/p/1502421/3557795.aspx/1?ASP+NET+MVC+2+beta+TryUpdateModel+not+working+ASP.NET MVC 2 beta -- TryUpdateModel not working <p>bradwil as&nbsp;suggested by you i tried the model binding in ASP.net MVC 2&nbsp;, but still not working.</p> <pre class="prettyprint">[MetadataType(typeof(PersonMetaData))] public class Person { public int PersonID { get; set; } public string Name { get; set; } } public class PersonMetaData { [Required] [Range(1, 100)] public int PersonID { get; set; } [Required] [StringLength(4)] public string Name { get; set; } }</pre><pre class="prettyprint"> [AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Person person) { if (person.PersonID &lt; 1) person.PersonID = 1; TryUpdateModel(person); if (ModelState.IsValid) { return View(person); } else { return View(person); } }</pre></PRE></PRE> <p>&nbsp;</p> <p>&nbsp;</p> <p>Please note in my Controller I am trying to change the value of person.Person=1 when the form input value is less than 1. But the validationstill fails. How do i update the model values before it does the validtion. quick response would be much appriciated. thanks.</p> 2009-12-10T07:43:39-05:003557977http://forums.asp.net/p/1502421/3557977.aspx/1?Re+ASP+NET+MVC+2+beta+TryUpdateModel+not+workingRe: ASP.NET MVC 2 beta -- TryUpdateModel not working <p>Validation fails because you're running it twice: once for the Person parameter, and once with the call to TryUpdateModel. The first time you're running the validation, it's failed because of the bad ID value.<br> </p> <p><br> </p> 2009-12-10T09:38:30-05:003559158http://forums.asp.net/p/1502421/3559158.aspx/1?Re+ASP+NET+MVC+2+beta+TryUpdateModel+not+workingRe: ASP.NET MVC 2 beta -- TryUpdateModel not working <p>&nbsp;if the first time validation is failing because of the bad PersonID value, <em> how I change the bad PersonID value in the Action</em> <strong>BEFORE</strong> <em> it gets validated</em>.&nbsp; </p> <p>I&nbsp;tried removing TryUpdateModel but the Validation still fails.&nbsp; Thanks .</p> <pre class="prettyprint">[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Person person) { if (person.PersonID &lt; 1) person.PersonID = 1; if (ModelState.IsValid) { return View(person); } else { return View(person); } }</pre> <p><br> &nbsp;</p> <p>&nbsp;</p> 2009-12-10T23:02:38-05:003559211http://forums.asp.net/p/1502421/3559211.aspx/1?Re+ASP+NET+MVC+2+beta+TryUpdateModel+not+workingRe: ASP.NET MVC 2 beta -- TryUpdateModel not working <p>That doesn't really seem like a logical thing to do. The user is entering bad data, so you want to fix it up with good data?</p> <p>Anyway, you can get what you want by inserting a call to ModelState.Clear() before the call to UpdateModel, which will clear out all model state (values &amp; errors). Or, if you just want to remove the errors for PersonID and not call the UpdateModel method the second time, you can call ModelState.Remove(&quot;PersonID&quot;).</p> 2009-12-11T00:08:34-05:003559647http://forums.asp.net/p/1502421/3559647.aspx/1?Re+ASP+NET+MVC+2+beta+TryUpdateModel+not+workingRe: ASP.NET MVC 2 beta -- TryUpdateModel not working <p>Hi,</p> <p>I am sorry that I can't find why you must use the 'TryUpdateModel'.</p> <p>It seems that you want to create some objects and store them in database, so you can use the following code.</p> <p><pre class="prettyprint">[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude = &quot;Id&quot;)] Movie movieToCreate) { MoviesDBEntities _db = new MoviesDBEntities(); if (!ModelState.IsValid) return View(); _db.AddToMovieSet(movieToCreate); _db.SaveChanges(); return RedirectToAction(&quot;Index&quot;); }</pre><br> Regarding the validation, I think you can validate data on the client with jquery.</p> <p><a href="http://weblogs.asp.net/cibrax/archive/2008/08/01/combining-jquery-validation-with-asp-net-mvc.aspx">http://weblogs.asp.net/cibrax/archive/2008/08/01/combining-jquery-validation-with-asp-net-mvc.aspx</a></p> 2009-12-11T06:19:43-05:00