I have tried using !Model.Any() it doesn’t work, since model has no extension Any. How to solve?
Here is my code snippet.
@model MyModel.Work
@if ( !Model.Any() )
{
<script type="text/javascript">
alert("Model empty");
</script>
}
else
{
<script type="text/javascript">
alert("Model exists");
</script>
}
It sounds to me like you are instantiating the model, but want to check and see if it’s been populated.
My standard way of doing this is to create a
boolproperty calledEmpty, only giving a get, and then return the check you need to see if no other properties have been set.Say you have a Customer class as your model:
Now in your model, you simply call: