我试图检查模型为null或不是,但我无法解决问题。 虽然呈现的主要观点我已经呈现局部视图如下检查模型是否为null或不在mvc
主视图
<div class="modal fade" id="surveyPreviewModal" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="surveyPreviewLabel" aria-hidden="true">
<div class="modal-lg modal-dialog">
<div class="modal-content" id="surveyPreviewContent">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
×
</button>
<h4 class="modal-title" id="surveyPreviewLabel">Survey Preview</h4>
</div>
<div class="modal-body" id="surveyPreviewBody">
@Html.Partial("_surveyPreview")
</div>
</div>
</div>
</div>
,并在局部视图我有功能如下
@model LMS_TraineeSurveyPaginationViewModel
<script type="text/javascript">
function SurveyPreview(){
var surveyQuestionViewModel = @Html.Raw(Json.Serialize(Model.SurveyQuestionsViewModel.ToArray()));
var surveyQuestionOptionChoideViewModel= @Html.Raw(Json.Serialize(Model.SurveyQuestionOptionChoiceViewModel.ToArray()));
$.post('@Url.Action("SurveyPreview", "Survey")', { SurveyID : surveyID,` page : page },
function (data) {
$('#surveyPreviewBody').html('');
$('#surveyPreviewBody').html(data);
SetProgressBar(page,'@(Model==null?0: Model.Pager.TotalPages)');
}).fail(function() {
alert("error in GetTraineeSurvey");
}).success(function() {
});
}
</script>
因此,虽然渲染局部在这个函数(SurveyPreview)中查看它正在给出错误,因为模型是空的,并且直接显示白色屏幕。如果我没有调用局部视图内部的函数,那为什么它会检查模型是否为null?它应该是什么时候我执行像按钮点击功能?
我在主视图中显示bootstrap模态和'show
'引导模态的方法,我再次返回相同的局部视图来绑定ajax调用中的数据。 下面的代码写在局部视图
$(document).ready(function() {
$('#surveyPreviewModal').on('show.bs.modal', function (e) {
surveyID = $(e.relatedTarget).attr('data-surveyID');
SurveyPreview(@SurveyPageTypePageNumber.StartPage,null);
});
})
和控制器
public ActionResult SurveyPreview(int SurveyID, int page)
{
------ some code ------
return PartialView("_SurveyPreview",viewModel);
}
在此感谢所有帮助!
你是否将一个实例分配给控制器中的'viewmodel'?即'viewmodel = new viewmodel();'? –
@ BarryO'Kane是..这样'变种视图模型=新LMS_TraineeSurveyPaginationViewModel() { SurveyQuestionsViewModel = SurveyQuestionsViewModel.Where(X => x.PageNumber == pager.CurrentPage).ToList(), SurveyQuestionOptionChoiceViewModel = SurveyQuestionIDsOptionChoice, Pager = pager };' – Rohit
好吧,当你调试的时候,如果你在创建后将鼠标悬停在viewmodel上,你会看到这个实例吗? –