【发布时间】:2013-06-18 04:15:09
【问题描述】:
我一直在努力寻找答案,但我无法确切地说出我做错了什么。
我有自己的模型的局部视图
@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 始终为空,并且我无法弄清楚我缺少什么来解决此问题,如果您需要其他任何信息,请告诉我。提前感谢您的帮助!
【问题讨论】:
-
您是否在控制器操作中设置了断点以查看是否将
CurrentNotification属性设置为有用的东西? LinqSingleOrDefault方法可能会返回null,这应该会导致其他问题。在return语句上设置一个中断,看看实际传入了什么,然后在局部视图中对if语句设置一个中断,看看你作为模型得到了什么。 -
曾经遇到过类似的问题,但后来发现我只是忘记在视图模型中的属性中添加 getter 和 setter
-
保存后是否重新加载通知?
-
@Corey - 是的,我已经确保它在使用 singleordefault 重新加载时不为空,甚至在 f12 控制台窗口中正确设置,hiddenfor 具有我相信的正确值
-
@AltafSami - 不,我在保存之前重新加载它。本质上它只是重新加载和替换部分视图。
标签: c# asp.net-mvc-3 razor viewmodel