c# - ModelState.isvalid returning false? -
i have userstats model, , user model, modelstate.isvalid in controller returning false, think may have reltionship between the 2 models 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 { { return calculatestats.calculatebmi(weight,height); } } [notmapped] public double bmr { //get { return calculatestats.calculatebmr(user.selectedgender, weight, height, user.age); } { 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 { { 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) </td> </tr> </table> <p> <input type="submit" value="create" /> </p> </fieldset> }
it seems models not connected. there no same id filed. may add userid
userstats
. , think registermale
method expects 1 model userstats
. rewrite 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); }
Comments
Post a Comment