Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ModelState.isvalid returning false?

I have a Userstats model, and a user model, the Modelstate.isvalid in the controller is returning false, I think it may have something to do with the reltionship between the the two models but i'm not sure

userstats model:

public class UserStats
{
    Calculator CalculateStats = new Calculator();


    public ActivityLevel ActivitySelected { get;set; }

    [Key]
    public int ID { get; set; }


    public  User User { get; set; }

    [DisplayName("Weight")]
    [Required]
    public double Weight { get; set; }

    [DisplayName("Chest Measurement")]
    [Required]
    public double ChestMeasurement { get; set; }

    [DisplayName("Hip Measurement")]
    [Required]
    public double HipMeasurement { get; set; }

    [DisplayName("Waist Measurement")]
    [Required]
    public double WaistMeasurement { get; set; }

    [DisplayName("Bicep Measurement")]
    [Required]
    public double BicepMeasurment { get; set; }

    [DisplayName("Height Measurement(Inches)")]
    [Required]
    public double Height { get; set; }


    [DisplayName("Body Fat %")]
    [NotMapped]
    public double BodyFat { get; set; }

    [NotMapped]
    public double BMI
    {
        get { return CalculateStats.CalculateBMI(Weight,Height); }
    }

    [NotMapped]
    public double BMR
    {
        //get { return CalculateStats.CalculateBMR(user.SelectedGender, Weight, Height, user.Age); }
        get { return 0; }


    }


    [DisplayName("Stats Log Date")]
    [Required]
    public DateTime StatDate { get; set; }

    [DisplayName("Target Weight")]
    [Required]
    public double TargetWeight { get; set; }

    [DisplayName("Target Date")]
    [Required]
    public DateTime TargetDate { get; set; }

    [DisplayName("Wrist Measurement(Inches)")]
    [Required]
    public double WristMeasurement { get; set; }

    [DisplayName("Forearm Measurement(Inches)")]
    [Required]
    public double ForeArm { get; set; }

    [DisplayName("Daily Caloric Intake")]
    [NotMapped]
    public double DailyIntake { get; set; }


    [DisplayName("Daily Allowance")]
    [NotMapped]
    public double DailyCalories { get; set; }


    [DisplayName("Lean Body Mass")]
    [NotMapped]
    public double LeanMass { get; set; }

}

user model:

 public class User
{
    [Key]
    public int ID { get; set; }



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

    [DisplayName("First Name")]
    [Required]
    public string FirstName { get; set; }

    [DisplayName("Last Name")]
    [Required]
    public string LastName { get; set; }

    [DisplayName("D.O.B")]
    [DataType(DataType.Date)]
    public DateTime DOB { get; set; }

    private int _age;
    [NotMapped]
    public int Age 
    {
        get { return _age; }
        set 
        { 
            DateTime today = DateTime.Today;
            _age = today.Year - DOB.Year;
            if (DOB > today.AddYears(-_age)) _age--; 
        }

    }


    [DisplayName("Address")]
    public string Address { get; set; }

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


    [Required]
    public Gender Gender { get; set; }

    [DisplayName("UserName")]
    public string UserName { get; set; }




    public Gender SelectedGender { get; set; }


    }
}

registeMale controller:

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult RegisterMale(User u,UserStats userstats)
    {

        if (ModelState.IsValid)
        {
            var user = db.Users.SingleOrDefault(i => i.ID == u.ID);
            userstats.User = user;
            db.UserStats.Add(userstats);
            db.SaveChanges();
            return RedirectToAction("Details", "Dashboard", new { id = userstats.ID });
        }

        return View(userstats);
    }

View:

   <fieldset>
        <legend>UserStats</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Weight)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Weight)
            @Html.ValidationMessageFor(model => model.Weight)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ChestMeasurement)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ChestMeasurement)
            @Html.ValidationMessageFor(model => model.ChestMeasurement)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.HipMeasurement)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.HipMeasurement)
            @Html.ValidationMessageFor(model => model.HipMeasurement)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.WaistMeasurement)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.WaistMeasurement)
            @Html.ValidationMessageFor(model => model.WaistMeasurement)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.BicepMeasurment)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.BicepMeasurment)
            @Html.ValidationMessageFor(model => model.BicepMeasurment)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Height)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Height)
            @Html.ValidationMessageFor(model => model.Height)
        </div>        
        <div class="editor-label">
            @Html.LabelFor(model => model.StatDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.StatDate)
            @Html.ValidationMessageFor(model => model.StatDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.TargetWeight)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.TargetWeight)
            @Html.ValidationMessageFor(model => model.TargetWeight)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.TargetDate)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.TargetDate)
            @Html.ValidationMessageFor(model => model.TargetDate)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.WristMeasurement)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.WristMeasurement)
            @Html.ValidationMessageFor(model => model.WristMeasurement)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ForeArm)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.ForeArm)
            @Html.ValidationMessageFor(model => model.ForeArm)
        </div>

       <table>
       <tr>
    <td>
        @Html.RadioButtonFor(model => model.ActivitySelected, Fitness_Friend.Web.Enumeration.ActivityLevel.Sedentary) Sedentary
    </td>
</tr>
<tr>
    <td>
        @Html.RadioButtonFor(model => model.ActivitySelected, Fitness_Friend.Web.Enumeration.ActivityLevel.LightActivity) Light Activity
    </td>
</tr>    
<tr>
    <td>
        @Html.RadioButtonFor(model => model.ActivitySelected, Fitness_Friend.Web.Enumeration.ActivityLevel.Moderate) Moderate
    </td>
</tr>
            <tr>
         <td>
        @Html.RadioButtonFor(model => model.ActivitySelected, Fitness_Friend.Web.Enumeration.ActivityLevel.Active) Active
    </td>
</tr>    
<tr>
    <td>
        @Html.RadioButtonFor(model => model.ActivitySelected, Fitness_Friend.Web.Enumeration.ActivityLevel.Extra) Extra
    </td>
</tr>
    </table>    
        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}
like image 508
CodeSurfer Avatar asked Nov 02 '22 15:11

CodeSurfer


1 Answers

It seems your models are not connected. There is no same id filed. You may add userID to UserStats. And I think your RegisterMale method expects only one model UserStats. Just rewrite it as follows

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult RegisterMale(UserStats model)
    {

        if (ModelState.IsValid)
        {
            var user = db.Users.SingleOrDefault(i => i.ID == model.userID );
            db.UserStats.Add(userstats);
            db.SaveChanges();
            return RedirectToAction("Details", "Dashboard", new { id = userstats.ID });
        }

        return View(userstats);
    }
like image 117
Andrey Gubal Avatar answered Nov 08 '22 09:11

Andrey Gubal