ModelState.isvalid返回false?

时间:2013-09-04 15:41:56

标签: c# asp.net-mvc asp.net-mvc-4 razor entity-framework-4

我有一个Userstats模型和一个用户模型,控制器中的Modelstate.isvalid返回false,我认为这可能与两个模型之间的相关性有关但我不确定

userstats模型:

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; }

}

用户模型:

 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控制器:

 [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);
    }

查看:

   <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>
}

1 个答案:

答案 0 :(得分:2)

您的型号似乎没有连接。没有相同的id提交。您可以将userID添加到UserStats。我认为您的RegisterMale方法只需要一个模型UserStats。只需将其重写如下

[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);
    }