【问题标题】:Checking model is null or not in mvc在 mvc 中检查模型是否为空
【发布时间】:2017-02-17 23:45:53
【问题描述】:

我正在尝试检查模型是否为空,但我无法解决问题。 在渲染主视图时,我将部分视图渲染如下

主视图

<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)中渲染局部视图时,它会给出错误,因为模型为空并且立即显示白屏。如果我没有调用局部视图内部的函数,那么它为什么要检查模型是否为空?应该是每当我执行按钮点击之类的功能时?

我在显示引导模式的主视图上有一个按钮,在引导模式的“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 是的.. 像这样var viewModel = new LMS_TraineeSurveyPaginationViewModel() { SurveyQuestionsViewModel = SurveyQuestionsViewModel.Where(x =&gt; x.PageNumber == pager.CurrentPage).ToList(), SurveyQuestionOptionChoiceViewModel = SurveyQuestionIDsOptionChoice, Pager = pager };
  • 好的,如果在创建实例后将鼠标悬停在viewmodel 上,则在调试时可以看到实例吗?
  • 是的.. 肯定的.. 但是当我渲染主/父视图并从该视图渲染部分视图时,最初会出现问题。在这种情况下,不调用 action 方法。

标签: c# asp.net-mvc razor asp.net-core asp.net-core-mvc


【解决方案1】:

当您使用 @Html.Partial("_surveyPreview") 加载部分视图时,它需要传递未提供的 LMS_TraineeSurveyPaginationViewModel

所以要调用部分视图,您需要编写类似

的内容
@Html.Partial("_surveyPreview",new LMS_TraineeSurveyPaginationViewModel());

【讨论】:

  • 是的..尝试了这种方式并解决了问题。谢谢!
【解决方案2】:

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

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

所以你需要确保局部视图得到模型。

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

在主视图中替换以下行

@Html.Partial("_surveyPreview")

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

这样我将使用提供的参数调用控制器的SurveyPreview 操作,它将返回带有模型的部分视图并进行渲染。

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

【讨论】:

  • 感谢您的帮助!
猜你喜欢
  • 2017-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-15
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 2023-01-09
相关资源
最近更新 更多