Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MVC3 Razor: how to check if model is empty

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>
    }
like image 610
MagB Avatar asked Dec 09 '11 16:12

MagB


People also ask

What is Razor syntax in MVC?

Razor is a markup syntax that lets you embed server-based code into web pages using C# and VB.Net. It is not a programming language. It is a server side markup language. Razor has no ties to ASP.NET MVC because Razor is a general-purpose templating engine. You can use it anywhere to generate output like HTML.

Does MVC use razor pages?

From the docs, "Razor Pages can make coding page-focused scenarios easier and more productive than using controllers and views." If your ASP.NET MVC app makes heavy use of views, you may want to consider migrating from actions and views to Razor Pages.

What is razor in MVC What are the main Razor syntax rules?

Razor is a simple programming syntax for embedding server code in web pages. Razor syntax is based on the ASP.NET framework, the part of the Microsoft.NET Framework that's specifically designed for creating web applications.

What is razor Visual Studio?

What is Razor? Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages. Server-based code can create dynamic web content on the fly, while a web page is written to the browser.


2 Answers

how about this:

if(Model == null)
{
}
like image 194
bobek Avatar answered Oct 20 '22 09:10

bobek


You can try this:

@if (Model.Count == 0)
{

}
like image 37
myaseedk Avatar answered Oct 20 '22 09:10

myaseedk