Mag*_*agB 9 razor asp.net-mvc-3
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>
}
Run Code Online (Sandbox Code Playgroud)
mcc*_*002 27
听起来像你在实例化模型,但想检查并查看它是否已填充.
我这样做的标准方法是创建一个bool名为的属性Empty,只给出一个get,然后返回你需要查看的检查,看看是否没有设置其他属性.
假设您有一个Customer类作为您的模型:
public class Customer
{
public int CustomerId {get;set;}
public string FirstName {get;set;}
public string LastName {get;set;}
public string Email {get;set;}
public bool Empty
{
get { return (CustomerId == 0 &&
string.IsNullOrWhiteSpace(FirstName) &&
string.IsNullOrWhiteSpace(LastName) &&
string.IsNullOrWhiteSpace(Email));
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在在您的模型中,您只需致电:
@model MyModel.Work
@if (Model.Empty)
{
<script type="text/javascript">
alert("Model empty");
</script>
}
else
{
<script type="text/javascript">
alert("Model exists");
</script>
}
Run Code Online (Sandbox Code Playgroud)
小智 15
你可以试试这个:
@if (Model.Count == 0)
{
}
Run Code Online (Sandbox Code Playgroud)
小智 6
@if(!Model.Any()){}当您传递数据列表作为模型时有效。如果您试图检查模型是否为空(不是列表并且可能包含单个记录或不包含任何记录),那么我通常使用@if(Model == null).
希望能帮助到你 :)
| 归档时间: |
|
| 查看次数: |
77707 次 |
| 最近记录: |