Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModelState.IsValid does not exclude required property

Tags:

asp.net-mvc

Im trying to exclude a required property(Password) so the modelstate dont validate that property, but for some reason it still validate even when i try to exclude it.

Controller:

    [Authorize, AcceptVerbs(HttpVerbs.Post)]
    public ActionResult _Edit(int id, [Bind(Exclude = "Password")]FormCollection collection)
    {
        var user = Proxy.GetUser(id);

        TryUpdateModel(user, null, null, new[]{"Password"});

        if(!ModelState.IsValid)
            return PartialView(user);

        Proxy.UpdateUser(user);
    }

View:

   ...
   <tr>
       <td class="label">
           <label class="row_description" for="Password"><%= S._("Password")%></label>
       </td>
       <td>
           <%= Html.Password("Password", null, new { @class = "row_input" })%>
           <%= Html.ValidationMessage("Password", "*")%>
       </td>
   </tr>

User(using dataannotation):

[Required]
public string Password { get; set; }

Im using VS2008, MVC2, firefox

Maybe im just tired and can't see it. Any help is appreciated

like image 226
larole Avatar asked Jul 02 '10 14:07

larole


People also ask

What exactly does ModelState IsValid do?

ModelState. IsValid indicates if it was possible to bind the incoming values from the request to the model correctly and whether any explicitly specified validation rules were broken during the model binding process.

Why ModelState IsValid is false in MVC?

That's because an error exists; ModelState. IsValid is false if any of the properties submitted have any error messages attached to them. What all of this means is that by setting up the validation in this manner, we allow MVC to just work the way it was designed.

Which property is used to determine an error in ModelState?

In ASP.NET 5 MVC, the ModelState property of a controller represents the submitted values, and validation errors in those values if such errors exist, during a POST action. During the POST, the values submitted can be validated, and the validation process uses attributes defined by . NET like [Required] and [Range] .

What is ModelState IsValid in asp net core?

Model state represents errors that come from two subsystems: model binding and model validation. Errors that originate from model binding are generally data conversion errors. For example, an "x" is entered in an integer field.


2 Answers

I am currently experiencing a similar issue with MVC3.

Despite [Bind(Exclude = "Password")] in my Action, ModelState.IsValid still returns false.

I noticed that TryUpdateModel(user, null, null, new string[]{"Password"}); was successfully updating the model; however still returning false. I then found out (somewhere on stackoverflow, apologies for not having the link) that TryUpdateModel actually returns ModelState.IsValid.

Thus, the issue is not with TryUpdateModel, but with ModelState.IsValid.

NB: this also means that you do not need to verify this twice... you can use this code:

if (!TryUpdateModel(user, null, null, new string[]{"Password"}))
    return PartialView(user);

The issue thus appears as if ModelState is still validating properties that have been excluded from your FormCollection.

I was able to overcome this by removing the field from ModelState prior to calling TryUpdateModel:

ModelState.Remove("Password");

Note that TryUpdateModel still requires the list of properties to exclude from the update as per the above code.

like image 118
Mark van Proctor Avatar answered Sep 24 '22 12:09

Mark van Proctor


I had success using the following method within ASP .NET MVC 2

TryUpdateModel(user);
ModelState.Remove("Password");
if (!ModelState.IsValid) return PartialView(user);

To keep the TryUpdate from binding to certain model properties you can create an inclusion template such as the one below:

public interface IUserValidateBindable
{
    string UserId { get; set; }
}

public class User : IUserValidateBindable
{
    [Required]
    public string UserId { get; set; }
    [Required]
    public string Password { get; set; }
}

The update the TryUpodateModel call as follows:

TryUpdateModel<IUserValidateBindable>(user);
like image 38
Roland Schaer Avatar answered Sep 23 '22 12:09

Roland Schaer