c# - Model is null in controller even after fixing get set in model definition -
i had application weakly typed , decided best change typed... running issues.
my controller delivers new model razor view (which has small form in it). use textboxfor(model => mymodel.type) , post controller. can see post has needed values once hits controller model null.
[httpget] public actionresult login(int op) { loginwrapperclass newoperation = new loginwrapperclass(); newoperation.newl = new epsdb(); newoperation.newp = new ep(op, request.cookies["state"].value); return view(newoperation); } [httppost, validateinput(true)] public actionresult login(loginwrapperclass newoperation) { if (modelstate.isvalid) { newoperation.lookup(); if (newoperation.newp != null) { //stuff return redirecttoaction("recordop", "authenticated", newoperation.newp); } } view:
@model webapp.models.loginwrapperclass @{ viewbag.title = "operation"; } <div id="indexop" class="textcenter"> <div id="time"></div> <div id="loader" class="hiddenandgone"><img src="~/content/images/loader.gif"></div> <div id="operations"> @using (html.beginform()) { //html.antiforgerytoken(); <div class="ops"> <div> @html.textboxfor(model => model.newl.badge_nbr, new { @id = "badge_nbr", @class = "readfont", autofocus = "autofocus", autocomplete = "off", placeholder = "" }) @html.textboxfor(model => model.newp.punch_type, new { type="number", @class = "hidden" }) </div> <div id="submit" class="hidden"> <input type="submit" value="validate" class="validatesubmit" /> </div> </div> } </div> </div> basically, through fiddler, can see post contains values need controller's model null. why?
what posting this: newl.badge_nbr=18845733&newp.punch_type=1
i adding class declarations here:
public class loginwrapperclass { public epsdb newl { get; set; } //fixed per aaron's response... still no binding public ep newp{ get; set; } public void lookup() { using (var db = new pstestentities()) { ienumerable<epsdb> foundindb; foundindb = db.epsdbs.where(founde => this.newl.badge_nbr.tostring() == founde.badge_nbr.tostring().substring(3)); if (foundindb.count() > 0) { this.newl = foundindb.first(); this.newp.badge_nbr = this.newl.badge_nbr; this.newp.emplid = this.newl.emplid; } } } } //this partial class added of public partial class epsdb { [key] public int p_key { get; set; } public int emplid { get; set; } [required] public long badge_nbr { get; set; } public nullable<system.datetime> birthdate { get; set; } public string name { get; set; } } public partial class ep { [required] public long badge_nbr { get; set; } [required] public int emplid { get; set; } [required] public system.datetimeoffset punch_dttm { get; set; } [required] public int punch_type { get; set; } public nullable<double> tl_quantity { get; set; } [key] public long p_key { get; set; } } public partial class ep { [notmapped] public bool containsquestions = false; [notmapped] public list<question> questions { get; set; } public ep(int punchtype, string state) { punch_dttm = datetimeoffset.utcnow; punch_type = punchtype; if((punchtype == 1 || punchtype == 2) & containsquestions) questions = getquestions(state, punchtype); } private list<question> getquestions(string state, int punchtype){ list<question> questions = new list<question>(); //do stuff return questions } ///plus other methods not used in controller/view } public class question { public string questionname { get; set; } public string questionstring { get; set; } public int answer { get; set; } }
you suffering believe: asp.net mvc - model binding excludes class fields?
you have declaration using fields:
public class loginwrapperclass { public epsdb newl; public ep newp; //... } but need using properties:
public class loginwrapperclass { public epsdb newl {get;set;} public ep newp {get;set;} //... }
Comments
Post a Comment