简体   繁体   English

在razor应用程序中测试空模型

[英]Testing a null model in razor application

i have an asp.net web application with razor view engine. 我有一个带有razor视图引擎的asp.net Web应用程序。 I'd like to test if the Model is null or not 我想测试Model是否为null

<center><h2 style="color:red">Les actualites des taches</h2></center>
<br />
<br />
@if(Model[0].Count == 0 || Model[0] == null) {
    <label>Pas de nouvelle information</label>
                         }
else{

foreach( var v in Model[0]){
    <div><label>@v</label></div>
   <br />
}

}
<br />
<br />
<br />
<br />
<center><h2 style="color:red">Demande de reservation de véhicules</h2></center>
<br />
<br />
@if (Model[1].Count == 0 || Model[1] == null)
{
    <label>Pas de demande</label>
                         }
else{
    <form action="\Travail\Validation_Reservation" method="post">
@foreach( var v in Model[1]){
    <div><label>@v</label>
    <input type="submit" name="ok" value="valider" />
   <input type="submit" name="No" value="refuser" /></div>
    <br />
}
        </form>
}
<br />
<br />

<center><h2 style="color:red">Demande de validation de tache</h2></center>
<br />
<br />
@if (Model[2].Count == 0 || Model[2] == null)
{
    <label>Pas de demande</label>
                         }
else{
    <form action="\Travail\Validation_Demande" method="post">
@foreach( var v in Model[2]){
    <div><label>@v</label>
    <input type="submit" name="ok" value="valider" />
   <input type="submit" name="No" value="refuser" /></div>
    <br />
}
        </form>
}
<br />
<br />
<center><h2 style="color:red">Déclarer l'absence ou la présence</h2></center>
<br />
<br />
<form action="\Travail\Validation_Absence" method="post">
<input type="submit" name="ok" value="je vais etre indisponible" />
<input type="submit" name="No" value="je suis de retour" /></div>
    <br />

        </form>

but an exception of null Model is appeared in this line @if(Model[0].Count == 0 || Model[0] == null) { . 但是在此行@if(Model[0].Count == 0 || Model[0] == null) {出现了null模型的异常。

So how can i fix this problem? 那么我该如何解决这个问题呢? Any suggestions? 有什么建议么?

Reverse the operands to check for null first: 反转操作数以首先检查是否为null

@if (Model == null || Model[0] == null || Model[0].Count == 0) {

Conditions in such expressions are checked subsequently, so that if first expression gives true (or false in case of && ) - further calculations are not performed. 随后检查此类表达式中的条件,以便如果第一个表达式给出true (或在&&情况下为false ),则不执行进一步的计算。 Therefore it is always better to check for null in first place, and only then use object for further checks. 因此,始终最好首先检查null ,然后再使用object进行进一步检查。

I would strongly suggest that you never pass a NULL model to your view. 我强烈建议您不要将NULL模型传递给视图。 Pass a viewmodel class which has the properties you intend to use in your view e..g 传递一个viewmodel类,该类具有您打算在视图中使用的属性,例如

public class MyViewModel
{
    IList<Reservation> Reservations { get; set; }

    public MyViewModel() {
        this.Reservations = new List<Reservation>();
    }
}

Then in your view you can do this 然后您认为您可以执行此操作

@model MyViewModel

if (!Model.Reservations.Any()) {
     <label>No reservations</label>
}

I appreciate my code may not match exactly what you have, but I hope the core idea comes through. 我很欣赏我的代码可能与您的代码不完全匹配,但是我希望核心思想能够通过。 I always do this with my views, I've never passed a NULL model to a view. 我总是使用视图来执行此操作,但从未将NULL模型传递给视图。 I think it's much cleaner to use a more descriptive way of laying out your view's logic, but using a viewmodel with well named properties. 我认为使用更具描述性的方法来布局视图的逻辑要干净得多,但要使用具有良好命名属性的视图模型。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM