0

I have an asp.net mvc4 application in which i'd like to use the fields validation :

My Model class :

namespace sample_mvc4.Models
{
    public class User
    {
        [Required(ErrorMessage="Enter le nom")]
        public string Name;
        [Required(ErrorMessage = "Enter l'émail")]
        public string Email;
        [Required(ErrorMessage = "pas de mdp!!!!")]

        public string Password;
    }
}

The Controller

public class HomeController : Controller
    {
       public ActionResult Index()
        {
            return View("Register");
        }

        public ActionResult Register(User u)
        {
            if (ModelState.IsValid)
            {
            string s = u.Name;
            return View("Index");
             }
            else
            {
                return View();
            }
        }
    }

And finally the view Register.cshtml

@model sample_mvc4.Models.User

@{
    Layout = null;
}

    <!DOCTYPE html>

    <html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <link type="text/css" href="~/Content/Site.css" />
        <title>Register</title>
    </head>
    <body>

            @using(Html.BeginForm("Register","Home")){
            @Html.ValidationSummary()
            <p> Name: @Html.TextBoxFor(x => x.Name)</p>
             <p> Email : @Html.TextBoxFor(x => x.Email)</p>
             <p> Password : @Html.TextBoxFor( x=>x.Password)</p>
                <input type="submit" value="Register" />
            }

    </body>
    </html>

My problem are :

  1. When i let a field or more empty , the error message isn't appear, why?
  2. In the action Register the User object's fields are always all null
  3. When i submitted the form , inside the action Register , the ModelState.IsValid takes always true

What are the reasons of these results? How can i fix my code?

1
  • 1
    Just a note. On success (when no errors to show) instead of return View(...) you should return RedirectToAction(...). Otherwise when you hit refresh the previous form will be resubmitted :) Commented Dec 22, 2013 at 23:51

2 Answers 2

2

The problem is that you use fields in your model. But Asp.net MVC uses properties for validation process. Update your model like this and it will works fine:

public class User
{
    [Required(ErrorMessage = "Enter le nom")]
    public string Name { get; set; }

    [Required(ErrorMessage = "Enter l'émail")]
    public string Email { get; set; }

    [Required(ErrorMessage = "pas de mdp!!!!")]
    public string Password { get; set; }
}

UPDATE1

You can update your controller like this:

    public ActionResult Index()
    {
        return RedirectToAction("Register");
    }

    public ActionResult Register()
    {
        return View("Register");   
    }


    public ActionResult Register(User u)
    {
        if (ModelState.IsValid)
        {
            string s = u.Name;
            return View("Index");
        }
        else
        {
            return View();
        }
    }

Or you can try to update your routes in RouteCollection, so default action will be Register and not Index

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks @Sergey . but i still have another problem when i put Html.BeginForm() in the place ofHtml.BeginForm("Register","Home") it's not working !!
Yes, it doesn't work as you return Register View for Index action. And by default it uses current action. So if you remove "Register","Home" parameters, then it will use "Index", "Home" parameters.
1

I believe the validation is checked on the post action. since you are redirecting it isn't checking. just building the new page. add a new method to your controller

[HttpPost]
public ActionResult Index(User model)
{
    if(ModelState.IsValid){
        return RedirectToAction("Register");
    }  
    else{
        return view(model);
    }
}

this will keep it on the page if there are validation issues

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.