2017-02-17 61 views
0

我试图检查模型为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"> 
        &times; 
       </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); 
} 

在此感谢所有帮助!

+0

你是否将一个实例分配给控制器中的'viewmodel'?即'viewmodel = new viewmodel();'? –

+0

@ BarryO'Kane是..这样'变种视图模型=新LMS_TraineeSurveyPaginationViewModel() { SurveyQuestionsViewModel = SurveyQuestionsViewModel.Where(X => x.PageNumber == pager.CurrentPage).ToList(), SurveyQuestionOptionChoiceViewModel = SurveyQuestionIDsOptionChoice, Pager = pager };' – Rohit

+0

好吧,当你调试的时候,如果你在创建后将鼠标悬停在viewmodel上,你会看到这个实例吗? –

回答

1

当您加载使用局部视图@ Html.Partial(“_ surveyPreview”),它需要LMS_TraineeSurveyPaginationViewModel传递未提供

所以叫局部视图,你需要写类似

@Html.Partial("_surveyPreview",new LMS_TraineeSurveyPaginationViewModel()); 
+0

是啊..试试这种方式,并解决了这个问题。谢谢 ! – Rohit

1

局部视图需要LMS_TraineeSurveyPaginationViewModel类型的模型。但是当您从主视图渲染partialview时,您没有传递任何模型对象。

在部分视图function SurveyPreview()使用模型的属性。由于您未从主视图传递任何模型对象,因此模型将在局部视图中显示为空。这就是为什么你看到NullReferenceException

所以你需要确保部分视图获取模型。

您需要采取不同的方法来渲染局部视图。您可以使用Html.Action来调用Action方法,该方法将返回部分视图并在主视图中呈现。

替换下在你的主视图

@Html.Partial("_surveyPreview") 

@Html.Action("SurveyPreview", new { SurveyID = "<<somesoveryId>>", page = "<<somepage>>"}) 

这样行,我会打电话与提供的参数控制器的SurveyPreview行动,它将返回局部视图模型和它会被渲染。

我不确定在SurveyIDpage参数中传递了什么值,所以我在这里放置了占位符。你需要在那里输入适当的值。

+0

感谢您的帮助! – Rohit