Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mvc model validation required not working on all fields

Tags:

asp.net-mvc

I'm working in ASP.NET MVC 4 and I have the problem that my model validation isn't working correctly. For some reason not all my required fields have to be filled in.

Here's my model:

public class MovieModel
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public DateTime ReleaseDate { get; set; }
        [Required]
        public string Genre { get; set; }
        [Required]
        public decimal Price { get; set; }

        public virtual ICollection<RoleInMovie> RoleInMovie { get; set; }
    }

Here's the View:

@using (Html.BeginForm())
{
    <table>
        <tr>
            <td>
                <label>Name:</label></td>
            <td>@Html.EditorFor(m => m.Name)</td>
            <td>@Html.ValidationMessageFor(m => m.Name)</td>
        </tr>
        <tr>
            <td>
                <label>Genre:</label></td>
            <td>@Html.EditorFor(m => m.Genre)</td>
            <td>@Html.ValidationMessageFor(m => m.Genre)</td>
        </tr>
        <tr>
            <td>
                <label>Price:</label></td>
            <td>@Html.EditorFor(m => m.Price)</td>
            <td>@Html.ValidationMessageFor(m => m.Price)</td>
        </tr>
    </table>
    <button type="submit">Submit</button>
}

And here's my action:

[HttpPost]
        public ActionResult Add(MovieModel model)
        {
            if(ModelState.IsValid)
            {
                return RedirectToAction("Index");
            }
            return View();
        }

Now here's the thing: as soon as I enter only a price, modelstate.isvalid becomes true. When hovering over my model, it sais both name and genre are null. Ofcourse they are required, but the validation doesn't work. Also, the validationmessagefor only works on price.

I hope I'm not overlooking something too ridiculous. Thanks for the help!

like image 927
whodares Avatar asked Feb 17 '26 13:02

whodares


1 Answers

Return the invalid model back to the view:

[HttpPost]
public ActionResult Add(MovieModel model)
{
    if(ModelState.IsValid)
    {
        return RedirectToAction("Index");
    }
    return View(model); // <----
}

Oh, and make sure that the required attribute is disallowing empty strings

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.requiredattribute.allowemptystrings.aspx

public class MovieModel
{
    public int Id { get; set; }

    [Required(AllowEmptyStrings = false)]
    public string Name { get; set; }

    public DateTime ReleaseDate { get; set; }

    [Required(AllowEmptyStrings = false)]
    public string Genre { get; set; }

    [Required]
    public decimal Price { get; set; }

    public virtual ICollection<RoleInMovie> RoleInMovie { get; set; }
}
like image 87
asawyer Avatar answered Feb 19 '26 04:02

asawyer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!