0

我一直在努力寻找答案,但我无法确切地说出我做错了什么。

我有自己的模型的部分视图

@model ViewModels.NotificationPartialViewModel
@if (Model.CurrentNotification != null)
{
    <input name="CurrentNotification" id="CurrentNotification" type="hidden" value="@Model.CurrentNotification"/>
    @Html.HiddenFor(m => m.CurrentNotification.NotificationType)
    @Html.HiddenFor(m => m.CurrentNotification.NotificationStart)
    @Html.HiddenFor(m => m.CurrentNotification.NotificationEnd)
    @Html.HiddenFor(m => m.TimeZoneOffset, new { @class = "timeZoneOffset" })
    if (Model.CurrentNotification.NotificationType == Model.DowntimeKey)
    {
    <div class="fieldList">
        <fieldset>
            <legend>@Resources.PageLabels.Legend_NotificationDowntime</legend>
            <div class="fieldWrapper">
                <div class="editor-label">@Html.Label(Resources.PageLabels.Label_StartNotification)</div>
                <div class="editor-field newline-wrapper wide">
                    @Html.EditorFor(m => m.NotificationStartDateTime)
                    @Html.ValidationMessageFor(model => model.NotificationStartDateTime)
                </div>
                <div class="editor-field device">
                    <input type="date" id="NotificationStartDateTime_DevicePicker" />
                </div>
            </div>
            <div class="fieldWrapper">
                <div class="editor-label">@Html.Label(Resources.PageLabels.Label_EndNotification)</div>
                <div class="editor-field newline-wrapper wide">
                    @Html.EditorFor(m => m.NotificationEndDateTime)
                    @Html.ValidationMessageFor(model => model.NotificationEndDateTime)
                </div>
                <div class="editor-field device">
                    <input type="date" id="NotificationEndDateTime_DevicePicker" />
                </div>
            </div>
        </fieldset>
    </div>
    }
    else if (Model.CurrentNotification.NotificationType == Model.HotfixKey)
    {       
    <div class="fieldWrapper">
        <div class="editor-label">
            <label for="DisplayHotfixNotification">@Resources.PageLabels.Label_DisplayHotfix</label>
        </div>
        <div class="editor-field">@Html.CheckBoxFor(m => m.DisplayHotfixNotification)</div>
    </div>
    }
}

我正在通过对包含部分视图的 ajax 调用使用下拉列表刷新视图:

function reloadNotificationPartial() {
    var notificationID = document.getElementById("SelectedNotification").value;

$.ajax(
{
    url: '@Url.Action("ReloadNotifications")?notificationID=' + notificationID,
    success: function (html) {
        //Refresh the notification list.
        $('#MainPanel').html(html);
    }
});

}

这是控制器动作:

public PartialViewResult ReloadNotifications(Guid? notificationID)
{
    if (Request.IsAjaxRequest())
    {
        NotificationPartialViewModel vm = new NotificationPartialViewModel();

        _notificationRepository = DependencyResolver.Current.GetService<INotificationRepository>();
        List<Notification> noteList = _notificationRepository.GetAllNotifications();

        vm.CurrentNotification = noteList.SingleOrDefault(n => n.NotificationType.Equals(notificationID));

        vm.CurrentNotification.NotificationStart = ConvertToBrowserTime(vm.CurrentNotification.NotificationStart,
                                                                         SecurityCookieManager.getTimeZoneOffset(
                                                                             this.HttpContext));

        vm.CurrentNotification.NotificationEnd = ConvertToBrowserTime(vm.CurrentNotification.NotificationEnd,
                                                                       SecurityCookieManager.getTimeZoneOffset(
                                                                           this.HttpContext));
        //Ajax Request, only return the partial view
        return PartialView("NotificationPartial", vm);
    }
    throw new NotImplementedException("Action is not supported in the manner it was called.");
}

所以我想做的是根据下拉列表保存两种类型的通知之一。当我保存信息时 CurrentNotification 始终为空,并且我无法弄清楚我缺少什么来解决此问题,如果您需要其他任何信息,请告诉我。在此先感谢您的帮助!

4

1 回答 1

0

似乎这是一个双重问题,一个是我没有在视图模型构造函数中创建一个新的 CurrentNotification,然后在我把它放进去之后,我写完了我刚刚创建的新的 CurrentNotification,用一个隐藏的替换了好对象与空的。

于 2013-06-18T21:21:45.253 回答