私はこれについての答えをかなり探しましたが、何が間違っているのか正確にはわかりません。
独自のモデルを持つ部分ビューがあります
@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 であり、この問題を解決するために何が欠けているのかを特定できませんでした。他に何か必要な場合はお知らせください。よろしくお願いします。