简体   繁体   English

如果模型为IF空-ASP.NET MVC4

[英]IF Null in Model - ASP.NET MVC4

Can I declare "IF" statement or something similar to Models in MVC, eg here is my code: 我可以声明“ IF”语句还是类似于MVC中的Models,例如,这是我的代码:

namespace SolrWeb.Models
{
    public class SolrVariables
    {
    [SolrField("sequentialid")]
    public long Sequ { get; set; }

    [SolrField("exchangestatus")]
    public int ExtId { get; set; }

    [SolrField("msgtype")]
    public ICollection<string> MsgT { get; set; }
    ...

So variable: 所以变量:

    [SolrField("msgtype")]
    public ICollection<string> MsgT { get; set; }
    -> this if is NULL return Default "0" else return "String" 

I forgot to put my next important code: 我忘了输入下一个重要代码:

...
@foreach (var row in Model)
{
<tr>
    <td>@row.Sequ</td>
    <td>@row.ExtId</td>
    <td>@row.MsgT.First()</td>
    Because of ".First" if is Null code brake, this is why I whant to have If
    in my Model, so if it is Null, it won't brake...

Thanks all for help 谢谢大家的帮助

You could do something like this: 您可以执行以下操作:

private ICollection<string> _msgT;

[SolrField("msgtype")]
public ICollection<string> MsgT 
{ 
    get 
    {
        if(_msgT == null)
        {
            return new ICollection<string>() With {"0"};
        }

        return new ICollection<string>() With {"String"};
    };

    set(ICollection<string> value)
    {
        _msgT = value
    };
 }

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

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