gpt4 book ai didi

c# - ModelState.isvalid 返回 false?

转载 作者:太空狗 更新时间:2023-10-29 23:39:25 26 4
gpt4 key购买 nike

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

用户统计模型:

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


}
}

注册男 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);
}

查看:

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

最佳答案

您的模型似乎没有连接。没有相同的 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);
}

关于c# - ModelState.isvalid 返回 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18618220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com