ViewModel子属性在POST时返回NULL

时间:2015-03-20 21:44:06

标签: c# jquery model-view-controller model-binding

我对MVC很新,遇到了一个我不明白的问题。这似乎是一种相当普遍的情况,但它对我来说不起作用。

我有2个模型和一个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; }
}

在我看来,当用户选择Recurring时,我添加了jQuery来弹出一个模态表单。保存属性常规HTML(非HTML帮助程序等)的div似乎允许用户选择正确的复选框/单选按钮,然后返回到主窗体。

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

这是弹出窗口的div和jQuery的代码。

                          @*This is the area where will will add the 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">
                                            &nbsp;
                                            @*@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">
                                            &nbsp;
                                            <label for="recurr_weekly">Weekly</label>
                                            <br>
                                            <input type="radio" name="Reoccurs.Frequency" value="Monthly" id="recurr_monthly">
                                            &nbsp;
                                            <label for="recurr_monthly">Monthly</label>
                                            <br>
                                        </div>
                                        <!-- END col_inleft -->
                                        <!-- Start section for Monthly settings, to hide section add hideselect to the existing class in the 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&nbsp;</span>
                                            <label for="Reoccurs.NthFrequency" class="hideme">Enter the recurring day placement frequency for a week, example, second, third, or last day of the 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 for 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">
                                            &nbsp;
                                            <label for="recurr_endafter" id="after">End after:</label>
                                            &nbsp;
                                            <input type="number" name="NthOccurences" value="" id="recurr_endafter" placeholder="1" aria-describedby="occ" max="10" disabled="disabled">
                                            <span class="matchlab" id="occ">&nbsp;occurences</span> <br>
                                            <input type="radio" name="EndBy" id="EndByDate" value="End by" aria-labelledby="roc by" disabled="disabled">
                                            &nbsp;
                                            <label for="recurr_endby" id="by">End by:&nbsp;<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 is the area where will will end the reoccurring information*@

jQuery代码:

  $(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);
    });
});

这是我的控制器:

      [HttpPost]
    public ActionResult Create(EventCreateViewModel model)
    {
//lots of checking, etc. going on here
var anything  = model.Reoccurs.Frequency; //Stop here check model
return RedirectToAction("Create");
}



 [HtmlGet]
 public ViewResult Create()
    {
        // Create the View Model for the DropDown List
      var model = new EventCreateViewModel();

        // Sets the new event start/end times
        model.Event = new Event();

        // Sets the new Reoccurence if necessary
        model.Reoccurs = new Reoccur();
       return View(model);

当我查看模型时,Reoccurs属性为NULL,即使View字段已填充了值(通过jQuery)。当我查看其他属性例如Event时,它会显示从View / Form中选择的字段。

您可以提供任何帮助,我非常感谢。我一直在努力让这项工作得以实现。我试图使用FormCollection,它也没有显示模态形式的属性(重新出现字段/属性)。

1 个答案:

答案 0 :(得分:0)

找出问题所在。对话框表单(通过jQuery)未附加到表单,因此表单中的值不会随帖子一起发送。我将此代码添加到jQuery对话框中,现在它正在运行。

                     open: function (type, data) {
                        $(this).parent().appendTo("#EventEntry");  
                    },

感谢您查看我的问题,希望这有助于某人!

相关问题