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??
bradwil as suggested by you i tried the model binding in ASP.net MVC 2 , but still not working.
[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; }
}
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Create(Person person)
{
if (person.PersonID < 1)
person.PersonID = 1;
TryUpdateModel(person);
if (ModelState.IsValid)
{
return View(person);
}
else
{
return View(person);
}
}
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.
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.
Marked as answer by ricka6 on Dec 11, 2009 03:39 PM
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?
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 & 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("PersonID").
Marked as answer by ricka6 on Dec 11, 2009 03:39 PM
ans_anish
Member
317 Points
110 Posts
TryUpdateModel not working in ASP.NET MVC 1
Dec 10, 2009 02:34 AM|LINK
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??
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude = "AccountElementID")]AccountElement accountElement) { accountElement.AccountID = 1; bool bol = TryUpdateModel(accountElement, new[] { "AccountID" }); if (ModelState.IsValid) { LoadUserControls(); return View(accountElement); } else { LoadUserControls(); return View(accountElement); } }bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: TryUpdateModel not working in ASP.NET MVC 1
Dec 10, 2009 06:32 AM|LINK
Support for DataAnnotations in new in MVC 2.
ans_anish
Member
317 Points
110 Posts
Re: TryUpdateModel not working in ASP.NET MVC 1
Dec 10, 2009 06:42 AM|LINK
how would i achive this in ASP.NET MVC 1. any work around????
ans_anish
Member
317 Points
110 Posts
ASP.NET MVC 2 beta -- TryUpdateModel not working
Dec 10, 2009 07:43 AM|LINK
bradwil as suggested by you i tried the model binding in ASP.net MVC 2 , but still not working.
[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; } }[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Person person) { if (person.PersonID < 1) person.PersonID = 1; TryUpdateModel(person); if (ModelState.IsValid) { return View(person); } else { return View(person); } }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.
bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: ASP.NET MVC 2 beta -- TryUpdateModel not working
Dec 10, 2009 09:38 AM|LINK
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.
ans_anish
Member
317 Points
110 Posts
Re: ASP.NET MVC 2 beta -- TryUpdateModel not working
Dec 10, 2009 11:02 PM|LINK
if the first time validation is failing because of the bad PersonID value, how I change the bad PersonID value in the Action BEFORE it gets validated.
I tried removing TryUpdateModel but the Validation still fails. Thanks .
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create(Person person) { if (person.PersonID < 1) person.PersonID = 1; if (ModelState.IsValid) { return View(person); } else { return View(person); } }bradwils
Contributor
5779 Points
691 Posts
Microsoft
Re: ASP.NET MVC 2 beta -- TryUpdateModel not working
Dec 11, 2009 12:08 AM|LINK
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?
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 & 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("PersonID").
KeFang Chen ...
Star
8329 Points
852 Posts
Re: ASP.NET MVC 2 beta -- TryUpdateModel not working
Dec 11, 2009 06:19 AM|LINK
Hi,
I am sorry that I can't find why you must use the 'TryUpdateModel'.
It seems that you want to create some objects and store them in database, so you can use the following code.
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Create([Bind(Exclude = "Id")] Movie movieToCreate) { MoviesDBEntities _db = new MoviesDBEntities(); if (!ModelState.IsValid) return View(); _db.AddToMovieSet(movieToCreate); _db.SaveChanges(); return RedirectToAction("Index"); }Regarding the validation, I think you can validate data on the client with jquery.
http://weblogs.asp.net/cibrax/archive/2008/08/01/combining-jquery-validation-with-asp-net-mvc.aspx