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.");
}

私がやろうとしているのは、ドロップダウン リストに基づいて 2 種類の通知のいずれかを保存することです。情報を保存し直すと、CurrentNotification は常に null であり、この問題を解決するために何が欠けているのかを特定できませんでした。他に何か必要な場合はお知らせください。よろしくお願いします。

4

1 に答える 1

0

ビュー モデル コンストラクターで新しい CurrentNotification を作成していないという 2 つの問題があるようでした。それを入れた後、作成したばかりの新しい CurrentNotification を、適切なオブジェクトを非表示のオブジェクトに置き換えて上書きしていました。ヌルのものと。

于 2013-06-18T21:21:45.253 に答える