I am using ASP.NET MVC Razor And Data Annotation validators
My model:
public class Person
{
public int id { get; set; }
[Required]
public string FirstName { get; set; }
[Required]
public string LastName { get; set; }
}
FirstName And LastName are Requerd.
I want to Edit FirstName. My Methode is:
public ActionResult Edit([Bind(Include = "FirstName")]Person person)
{
var p = GetPerson();
if (TryUpdateModel(p))
{
//Save Changes;
}
}
But TryUpdateModel always return false. because LastName is Invalid.
How Can I Prevent check Validation Of LastName in TryUpdateModel?
Note:
- The code is Simplified. my real code is very Complex
- I have To Use Requierd For Two Property
- I dont Want to use Different Model Class
I found Nice Solution. I must remove unused Field from ModelState. then ModelState.IsValid return true. first I need Create New Attribute class:
then I Add Attribute on my methode:
Look at this: http://blog.stevensanderson.com/2010/02/19/partial-validation-in-aspnet-mvc-2/