简体   繁体   中英

Testing a null model in razor application

i have an asp.net web application with razor view engine. I'd like to test if the Model is null or not

<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) { .

So how can i fix this problem? Any suggestions?

Reverse the operands to check for null first:

@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. Therefore it is always better to check for null in first place, and only then use object for further checks.

I would strongly suggest that you never pass a NULL model to your view. Pass a viewmodel class which has the properties you intend to use in your view e..g

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. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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