Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在视图中显示模型时,如何绕过不可为null的属性?_C#_Asp.net Mvc_Entity Framework_Ef Code First - Fatal编程技术网

C# 在视图中显示模型时,如何绕过不可为null的属性?

C# 在视图中显示模型时,如何绕过不可为null的属性?,c#,asp.net-mvc,entity-framework,ef-code-first,C#,Asp.net Mvc,Entity Framework,Ef Code First,因此,我的模型中有一个属性: public string ImageBase64 => System.Convert.ToBase64String(ImageData); 我不能通过添加一个?在字符串部分之后,因为我得到: 类型字符串必须是不可为Null的类型,才能将其用作泛型类型或方法Nullable中的参数 在我看来,我尝试了以下条件,其中workedor不起作用,我仍然得到了值不能为null。 参数名称:F5上的inArray: 如果属性为null,我怎么能忽略它,而仍然显示其余的

因此,我的模型中有一个属性:

public string ImageBase64 => System.Convert.ToBase64String(ImageData);
我不能通过添加一个?在字符串部分之后,因为我得到:

类型字符串必须是不可为Null的类型,才能将其用作泛型类型或方法Nullable中的参数

在我看来,我尝试了以下条件,其中workedor不起作用,我仍然得到了值不能为null。 参数名称:F5上的inArray:


如果属性为null,我怎么能忽略它,而仍然显示其余的属性呢

您收到的错误表明您的ImageData为空或格式错误。我相信失败的是System.Convert.ToBase64方法

此外,您不能添加?对于字符串类型,因为它已经可以为null,基本上就是您得到的编译消息所说的

图像数据来自哪里?它是字节数组吗

编辑以在问题后包含答案:

public string ImageBase64
{
    get
    {

        return this.ImageData != null ? Convert.ToBase64String(this.ImageData) : null;
    }
}

您可以使用以下内容:

    public string ImageBase64 => ( imagedata == null )?
String.Empty:System.Convert.ToBase64String(imagedata);

我不清楚到底是什么导致了这个错误,但是如果你需要检查空字符串,你可以使用:@if!string.IsNullOrWhiteSpaceModel.ImageBase64是的,它是一个byt数组。我不能补充?。我在问题中说了为什么。但是字节数组是空的吗?如果为空,就不要进行转换。然后,您将能够检查ImageBase64本身是否为空,而不会出现错误。是的,字节数组为空。你能在回答中说明你的意思吗。我是个新手,不知道根据你的建议做什么。
public string ImageBase64
{
    get
    {

        return this.ImageData != null ? Convert.ToBase64String(this.ImageData) : null;
    }
}
    public string ImageBase64 => ( imagedata == null )?
String.Empty:System.Convert.ToBase64String(imagedata);