c# - ViewModel child properties coming back as NULL on POST -
i'm new mvc , have encountered problem not understand. seems common situation it's not working me.
i have 2 models , 1 viewmodel
public class event { public int eventid { get; set; } public string subject { get; set; } public string description { public string address1 { get; set; } public string city { get; set; } public string state { get; set; } public string zip { get; set; } public string phone { get; set; } public datetime startdate { get; set; } public datetime enddate { get; set; } public string email { get; set; } public string description { get; set; } public bool recurring { get; set; } } public class reoccur { public int reoccuranceid { get; set; } public int duration { get; set; } public string frequency { get; set; } public string nthfrequency { get; set; } public string nthday { get; set; } public bool weekend { get; set; } public int nthoccurrences { get; set; } public int eventid { get; set; } public virtual event event { get; set; } } public class eventcreateviewmodel { public string starttime { get; set; } public string endtime { get; set; } public event event { get; set; } public reoccur reoccurs { get; set; } public list<datetime> startdates { get; set; } public list<datetime> enddates { get; set; } }
on view, added jquery pop-up modal form when user selects recurring. div holds properties regular html (non-html helpers, etc) appears allow user select proper checkboxes/radio buttons return main form.
@using (html.beginform()) { @html.antiforgerytoken() if (tempdata["success"] != null) { <div class="alert alert-success"><center>@tempdata["success"]</center></div> } if (tempdata["failure"] != null) { <div class="alert alert-danger"><center>@tempdata["failure"]</center></div> } <div class="form-horizontal"> @html.validationsummary(true) <h3>event details</h3> <div class="form-group"> @html.labelfor(model => model.event.subject, new { @class = "control-label col-md-3" }) <div class="col-md-9"> @html.textboxfor(model => model.event.subject, new { data_bind = "value: subject", style = "width: 300px;", size = 200 , placeholder = "3rd annual poetry slam" }) @html.validationmessagefor(model => model.event.subject) </div> </div> <div class="form-group"> @html.labelfor(model => model.event.description, new { @class = "control-label col-md-3" }) <div class="col-md-9"> @html.textareafor(model => model.event.description) @html.validationmessagefor(model => model.event.description) </div> <script type="text/javascript"> ckeditor.replace('event.description'); </script> </div> <div class="form-group"> @html.labelfor(model => model.event.startdate, new { @class = "control-label col-md-3" }) <div class="col-md-9"> @html.editorfor(model => model.event.startdate) @html.validationmessagefor(model => model.event.startdate) @html.dropdownlistfor(x => x.starttime, model.times, new { @id = "starttime" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.event.enddate, new { @class = "control-label col-md-3" }) <div class="col-md-9"> @html.editorfor(model => model.event.enddate) @html.validationmessagefor(model => model.event.enddate) @html.dropdownlistfor(x => x.endtime, model.times, new { @id = "endtime" }) </div> </div> <div class="form-group"> @html.labelfor(model => model.event.address1, new { @class = "control-label col-md-3" }) <div class="col-md-9"> @html.editorfor(model => model.event.address1, new { data_bind = "value: address1" }) @html.validationmessagefor(model => model.event.address1) </div> </div> <div class="form-group" id="zipbox"> @html.labelfor(model => model.event.zip, new { @class = "control-label col-md-3" }) <div class="col-md-9"> @html.editorfor(model => model.event.zip, new { data_bind = "value: zip" }) @html.validationmessagefor(model => model.event.zip) </div> </div> <div class="form-group" id="citybox"> @html.labelfor(model => model.event.city, new { @class = "control-label col-md-3" }) <div class="col-md-9"> @html.editorfor(model => model.event.city, new { data_bind = "value: city" }) @html.validationmessagefor(model => model.event.city) </div> </div> <div class="form-group" id="statebox"> @html.labelfor(model => model.event.state, new { @class = "control-label col-md-3" }) <div class="col-md-9"> @html.editorfor(model => model.event.state, new { data_bind = "value: state" }) @html.validationmessagefor(model => model.event.state) </div> </div> <div class="form-group"> @html.labelfor(model => model.event.phone, new { @class = "control-label col-md-3" }) <div class="col-md-9"> @html.editorfor(model => model.event.phone) @html.validationmessagefor(model => model.event.phone) </div> </div> <div class="form-group"> @html.labelfor(model => model.event.email, new { @class = "control-label col-md-3" }) <div class="col-md-9"> @html.editorfor(model => model.event.email) @html.validationmessagefor(model => model.event.email) </div> </div> <div class="form-group"> <div class="col-md-offset-2 col-md-9"> <input type="submit" value="create" class="btn btn-default" /> </div> </div> </fieldset> </div>
here div popup , code jquery.
@*this area will add reoccurring information*@ <div id="reoccur"> <div class="form-group"> @html.labelfor(model => model.event.recurring, new { @class = "control-label col-md-3"}) <div class="col-md-9"> @html.editorfor(model => model.event.recurring) @html.validationmessagefor(model => model.event.recurring) </div> </div> <div id="dialogform" style="display: none;"> <fieldset class="recurr"> <legend>recurring frequency</legend> <div class="clearboth"> @*<input type="text" name="reoccurs.frequency" value="sample" id="reoccurs.frequency" />*@ <div class="col_label"> recurrence pattern </div> <!-- end col_label --> </div> <!-- end clearboth --> <div class="clearboth"> <div class="col_inleft"> <input type="radio" name="reoccurs.frequency" value="daily" id="recurr_daily"> @*@html.radiobutton("reoccurs.frequency", "daily", new {id = "recurr_daily"})*@ <label for="recurr_daily">daily</label> <br> <input type="radio" name="reoccurs.frequency" value="weekly" id="recurr_weekly"> <label for="recurr_weekly">weekly</label> <br> <input type="radio" name="reoccurs.frequency" value="monthly" id="recurr_monthly"> <label for="recurr_monthly">monthly</label> <br> </div> <!-- end col_inleft --> <!-- start section monthly settings, hide section add hideselect existing class in div, ex. class="col_f3 hideselect" --> <div class="col_f3"> <input type="radio" id="weekend" value="true" class="hideme"> <label for="weekend" class="hideme">week days only</label> <input id="pattern" type="checkbox" name="pattern" value="" disabled="disabled" class="hideme"> <span class="matchlab">the </span> <label for="reoccurs.nthfrequency" class="hideme">enter recurring day placement frequency week, example, second, third, or last day of week.</label> <select id="reoccurs.nthfrequency" name="reoccurs.nthfrequency" disabled="disabled"> <option value="1">first</option> <option value="2">second</option> <option value="3">third</option> <option value="4">fourth</option> <option value="5">last</option> </select><br /> <input type="checkbox" name="reoccurs.nthday" id="monday" value="monday" /> <label for="monday">monday</label> <input type="checkbox" name="reoccurs.nthday" id="tuesday" value="tuesday" /> <label for="tuesday">tuesday</label> <input type="checkbox" name="reoccurs.nthday" id="wednesday" value="wednesday" /> <label for="wednesday">wednesday</label> <input type="checkbox" name="reoccurs.nthday" id="thursday" value="thursday" /> <label for="thursday">thursday</label> <input type="checkbox" name="reoccurs.nthday" id="friday" value="friday" /> <label for="friday">friday</label> <input type="checkbox" name="reoccurs.nthday" id="saturday" value="saturday" /> <label for="saturday">saturday</label> <input type="checkbox" name="reoccurs.nthday" id="sunday" value="sunday" /> <label for="sunday">sunday </label> </div> <!-- end col_f3 --> <!-- end section monthly settings--> </div> <!-- end clearboth --> <div class="clearboth"> <div class="col_label" id="roc"> range of recurrence </div> <!-- end col_label --> </div> <!-- end clearboth --> <div class="clearboth"> <div class="col_da"> <input type="radio" name="endby" id="endby" value="end after" aria-labelledby="roc after" disabled="disabled"> <label for="recurr_endafter" id="after">end after:</label> <input type="number" name="nthoccurences" value="" id="recurr_endafter" placeholder="1" aria-describedby="occ" max="10" disabled="disabled"> <span class="matchlab" id="occ"> occurences</span> <br> <input type="radio" name="endby" id="endbydate" value="end by" aria-labelledby="roc by" disabled="disabled"> <label for="recurr_endby" id="by">end by: <span class="hideme" id="caldate">calendar date</span></label> <input id="recurr_endby" type="date" name="end_range_recur" placeholder="wed 5/1/2013" aria-describedby="caldate" disabled="disabled"> <br> </div> <!-- end col_in7 --> </div> <!-- end clearboth --> </fieldset> </div> </div> @*this area will end reoccurring information*@
jquery code:
$(function () { $('#recurr_daily').click(function () { $('input[name="reoccurs.nthday"]').prop('checked', true); $('#reoccurs\\.nthfrequency').prop('disabled', true); }); $('#recurr_weekly').click(function () { $('input[name="reoccurs.nthday"]').prop('checked', false); $('#reoccurs\\.nthfrequency').prop('disabled', true); }); $('#recurr_monthly').click(function (e) { $('input[name="reoccurs.nthday"]').prop('checked', false); $('#reoccurs\\.nthfrequency').attr('disabled', false); }); });
here's controller:
[httppost] public actionresult create(eventcreateviewmodel model) { //lots of checking, etc. going on here var = model.reoccurs.frequency; //stop here check model return redirecttoaction("create"); } [htmlget] public viewresult create() { // create view model dropdown list var model = new eventcreateviewmodel(); // sets new event start/end times model.event = new event(); // sets new reoccurence if necessary model.reoccurs = new reoccur(); return view(model);
when @ model, reoccurs property it's null, though view fields have been populated values (via jquery). when @ other properties instance event, shows fields chosen view/form.
any can provide, appreciate it. i've been struggling trying working. tried use formcollection , did not show properties modal form (reoccurs fields/properties).
figured out problem was. dialog form (via jquery) not attached form, therefore values form not sent post. added code jquery dialog , working.
open: function (type, data) { $(this).parent().appendto("#evententry"); },
thank looking @ question, hope helps someone!
Comments
Post a Comment