asp.net - No parameterless constructor defined for this object. MVC4 Razor, already elminated any parameters in constructor -
update: added bind exclude select list items.
now, i'm getting there no viewdataitem of type ienumerable has key blah blah...
if can't use viewmodel select lists, point?
getting error on post action. know has been asked before, 've followed instructions no avail. thoughts wrong? not sure if should include view or not...
here model:
public class outreach {
[displayname("number of mailings")] [range(1, 100000,errormessage="please positive number number of mailings")] public int mailings { get; set; } [displayname("number of calls")] public int pcalls { get; set; } [displayname("number of emails")] public int emails { get; set; } [displayname("number of walkins")] public int walkins { get; set; } [displayname("number of faxes")] public int faxes { get; set; } public int osid { get; set; } public int month { get; set; } public int year { get; set; } public outreach() { } } }
here viewmodel:
public class outreachviewmodel { [displayname("outreach specialist")] public selectlist outreachspecialist{ get; set; } public selectlist year { get; set; } public selectlist month { get; set; } public outreach out {get; set;} }
here controller:
public actionresult create() { list<tbloutreachspecialist> spec = repo.getallspecialists(); list<pyear> years=repo.getallyears(); list<pmonth> months=repo.getallmonths(); outreachviewmodel vw = new outreachviewmodel(); vw.year = new selectlist(years, "id", "pyear1"); vw.month = new selectlist(months, "id", "pmonth1"); vw.outreachspecialist = new selectlist(spec, "osid", "lastname"); return view(vw); } [httppost] public actionresult create(outreachviewmodel vm) { if (modelstate.isvalid) { repo.add(vm.out); repo.save(); } return view(vm); }
consider using different view model post method.
public class outreachviewmodelforcreate { public outreach out { get; set; } }
then, have post action take simpler view model. if model state isn't valid , need re-show view new user input, can call method convert create view model instance of display view model:
[httppost] public actionresult create(outreachviewmodelforcreate result) { if (modelstate.isvalid) { //write repo } var displayvm = getcreatedisplayviewmodel(); displayvm.out = result.out; return view(displayvm); }
you use custom model binder, prefer pattern.
update: see went bind exclude. i'll leave here in case.
Comments
Post a Comment