Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net MVC 3 Validation exclude some field validation in TryUpdateModel

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
like image 219
Morteza Avatar asked Mar 07 '12 15:03

Morteza


4 Answers

I found Nice Solution. I must remove unused Field from ModelState. then ModelState.IsValid return true. first I need Create New Attribute class:

public class ValidateOnlyIncomingValuesAttribute : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var modelState = filterContext.Controller.ViewData.ModelState;
        var valueProvider = filterContext.Controller.ValueProvider;

        var keysWithNoIncomingValue = modelState.Keys.Where( x=>!valueProvider.ContainsPrefix(x) );
        foreach (var key in keysWithNoIncomingValue)
            modelState[key].Errors.Clear();
    }
}

then I Add Attribute on my methode:

[ValidateOnlyIncomingValuesAttribute]
public ActionResult Edit([Bind(Include = "FirstName")]Person person)
{
    var p = GetPerson();
    if (ModelState.IsValid)
    {
        TryUpdateModel(p);
        //Save Changes;
    }
}

Look at this: http://blog.stevensanderson.com/2010/02/19/partial-validation-in-aspnet-mvc-2/

like image 162
Morteza Avatar answered Oct 19 '22 07:10

Morteza


You can remove the properties you don´t need before checking if the model is valid

ModelState.Remove("Email");
if (ModelState.IsValid)
{
   // whatever you want to do
}
like image 25
Priyanka Avatar answered Oct 19 '22 07:10

Priyanka


A very simple solution that I figured out.

public ActionResult Edit(Person person)
{
    ModelState.Remove("FirstName"); // This will remove the key 
    var p = GetPerson();
    if (TryUpdateModel(p))
       {
           //Save Changes;
       }
    }
}
like image 30
Desmond Avatar answered Oct 19 '22 09:10

Desmond


Short Answer: You can't, not using the default Data Annotations.

Longer Answer: You have several options.

  1. You can create your own validator annotations.
  2. You can make your model class inherit from IValidatableObject and then implement the Validate method. (this does not do client-side validation, however).
  3. You can use a third party validation library such as FluentValidation.
  4. You can create a different model for this scenario.
  5. You can remove the validation, and just add code to your action method that checks whether the fields are correct. (this is not recommended, since this increases the complexity of your controller action, and you have to duplicate this functionality anywhere you're using this model).
like image 28
Erik Funkenbusch Avatar answered Oct 19 '22 07:10

Erik Funkenbusch