我看起来很难找到答案,我无法确切地说出我做错了什么。
我有一个部分视图,有自己的模型
@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总是为空,我无法弄清楚我缺少什么来解决这个问题,如果你需要什么,请告诉我。在此先感谢您的帮助!
答案 0 :(得分:0)
这似乎是一个双重问题,一个我没有在视图模型构造函数中创建一个新的CurrentNotification,然后我把它放在我写的新的CurrentNotification,我刚刚用隐藏的一个替换带有null的好对象。