mporks
mporks

Reputation: 47

ModelState.IsValid keeps coming back as false?

SO I have a view where a member enters the PIN associated with them to clock in

@model Models.Member
@{
    Layout = "~/Views/Shared/_HomeLayout.cshtml";
}

<h1 style="margin-top: 0px;">Club Members Login Below!</h1> @*add this to the style for better ipad title -> "text-align: center;"*@
</br>
@using (Html.BeginForm("ClubHours", "Login", FormMethod.Post))
{
    @Html.LabelFor(c => c.PIN)
    @Html.TextBoxFor(c => c.PIN)<br />
    @Html.ValidationMessageFor(c => c.PIN)<br />

    <input type="submit" name="submit" value="ClockIn" />
    <input type="submit" name="submit" value="ClockOut" />
}

which interacts with this action result:

[HttpPost]
public ActionResult ClubHours(string submit, Member member)//member clocking in
{
    if (submit.Equals("ClockIn"))
    {
        if (!ModelState.IsValid) //validating events fields
        {
            return View("UserLogin");
        }
        else
        {
            var mem = _context.Members.SingleOrDefault(c => c.PIN == member.PIN);
            var hours = new MemberClubHours();
            hours.ClockIn = DateTime.Now;
            mem.Hours.Add(hours);
            _context.SaveChanges();
            return View("ClockIn");
        }
    }
    else if (submit.Equals("ClockOut"))
    {
        if (!ModelState.IsValid) //validating events fields
        {
            return View("UserLogin");
        }
        else
        {
            var mem = _context.Members.SingleOrDefault(c => c.PIN == member.PIN);
            var hours = new MemberClubHours();
            hours.ClockOut = DateTime.Now;
            mem.Hours.Add(hours);
            _context.SaveChanges();
            return View("ClockOut");
        }
    }
    else
    {
        return View("UserLogin","Login");
    }
}

and lastly here is that Member class

public class Member
{
    public int Id { get; set; }
    [Required]
    [MaxLength(4, ErrorMessage = "PIN must be 4 numbers long"), MinLength(4, ErrorMessage = "PIN must be 4 numbers long")]
    public string PIN { get; set; }
    [Required]
    [Display(Name ="First Name")]
    public string FirstName { get; set; }
    [Required]
    [Display(Name = "Last Name")]
    public string LastName { get; set; }
    [Display(Name = "Date of Birth")]
    public DateTime? Birthdate { get; set; }
    public virtual ICollection<MemberClubHours> Hours { get; } = new HashSet<MemberClubHours>();
}

and the memberclubhours class

public class MemberClubHours
{
    public int Id { get; set; }
    public DateTime? ClockIn { get; set; }
    public DateTime? ClockOut { get; set; }
    [Required]
    public Member Member { get; set; }
}

The code works correctly and will track the hours of a member, however I'm trying to implement validation, but even if I enter a PIN that is associated with a member in the system, it comes back as not valid? any help would be appreciated!

Upvotes: 0

Views: 431

Answers (2)

Jerdine Sabio
Jerdine Sabio

Reputation: 6140

Member model is coming out as invalid because the member Model requires the Id, PIN, FirstName, LastName fields.

You can't validate this class with ModelState.IsValid because it will check all the properties-- and it looks like you're only passing the PIN property.

If you don't want to make a viewModel for it, you could just include those properties as hidden inputfields;

@using (Html.BeginForm("ClubHours", "Login", FormMethod.Post))
{
    @Html.HiddenFor(c=>c.Id)
    @Html.HiddenFor(c=>c.FirstName)
    @Html.HiddenFor(c=>c.LastName)

    @Html.LabelFor(c => c.PIN)
    @Html.TextBoxFor(c => c.PIN)<br />
    @Html.ValidationMessageFor(c => c.PIN)<br />

    <input type="submit" name="submit" value="ClockIn" />
    <input type="submit" name="submit" value="ClockOut" />
}

Upvotes: 1

RealYlem
RealYlem

Reputation: 25

You're validating the model sent from the frontend. Not to the database, are you sure the model is populated according to your Data Annotations in the model?

Check out ModelState it's asp.net core 3.1 but still relevant

Upvotes: 1

Related Questions