1

ModelState.IsValid is always returning true.

Code:

user.cs

public class User
{

    public int UserID { get; set; }

    public string Username { get; set; }
    public string FirstName { get; set; }

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

HomeController.cs

public ActionResult SaveUser(User user)
    {
        if (ModelState.IsValid)
        {
            //create DBContext object
            using (var dbCtx = new UsersDbEntities())
            {
                dbCtx.Entry(user).State = EntityState.Modified;

                dbCtx.SaveChanges();
            }
            return View("ShowUser", user);
        }
        return View("EditUser", user);

    }

Register.cshtml

     @model TrainingWebsite.Models.User

<div id="myForm">
    @using (Html.BeginForm("RegisterUser", "Home", FormMethod.Post))
    {
    if (@ViewBag.Message != null)
    {
        <div style="border: 1px solid red">
            @ViewBag.Message
        </div>
    }
    <table>
        <tr>
            <td>@Html.LabelFor(a => a.Username)</td>
            <td>@Html.TextBoxFor(a => a.Username, new { id = "id_username" })</td>
            <td>@Html.ValidationMessageFor(a => a.Username)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(a => a.FirstName)</td>
            <td>@Html.TextBoxFor(a => a.FirstName, new { id = "id_firstName" })</td>
            <td>@Html.ValidationMessageFor(a => a.FirstName)</td>
        </tr>
        <tr>
            <td>@Html.LabelFor(a => a.LastName)</td>
            <td>@Html.TextBoxFor(a => a.LastName, new { id = "id_lastName" })</td>
            <td>@Html.ValidationMessageFor(a => a.LastName)</td>
        </tr>
 </table>

Register form

ModelState.IsValid is true even though Last Name field is empty yet is a required field ModelState.IsValid = true

Any help would be appreciated. Thanks in advance

David

  • 1
    One quick thing to check, is the `User` model within same namespace in both action methods you posted: `SaveUser` and `RegisterUser` ? I think the `User` in screenshot should be from Entity Framework model which might not have the `[Required]` attribute? – Siva Gopal Dec 18 '15 at 14:41
  • The User model is in the namespace 'TrainingWebsite.Models' and the controller method is in the namespace 'TrainingWebsite.Controllers' I've added using directive 'using TrainingWebsite.Models;' to the controller class, however it is greyed out as an unused directive. Could this be relevant to my issue? – davidpepper223 Dec 18 '15 at 16:47
  • You clearly have 2 models with the name `User` and the one in the `RegisterUser()` method is not the same as the one in the view (and it would not make sense to save an object with properties such as `ConfirmPassword` to a database. Change the name of the model in your view to (say) `UserViewModel` or `RegisterViewModel` so its clear its a view model and avoid the conflict (it should also be in a separate folder folder - not your Models folder) –  Dec 18 '15 at 20:58
  • You have to block empty strings. [This thread should answer your question.](https://stackoverflow.com/questions/23939738/how-can-i-use-data-annotations-attribute-classes-to-fail-empty-strings-in-forms?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) – IsThisThingOn May 09 '18 at 17:45

0 Answers0