I have an image property in my model, which sometimes migh be empty. The problem is that when there is no image in the database I gat an argumentNull exception
. What I'd like to achieve is still to display the rest of the properties if there is no image in the record.
For the purpose I tried doing something like this in my view, which didn't work :
<div>
<h4>Company</h4>
<hr />
<dl class="dl-horizontal">
<dt>
@Html.DisplayNameFor(model => model.ImageData)
</dt>
@if (Model.ImageBase64 != null || Model.ImageData!= null )
{
<dd>
<img src="data:image/png;base64,@Model.ImageBase64" />
</dd>
}
<dt>
@Html.DisplayNameFor(model => model.CompanyName)
</dt>
<dd>
@Html.DisplayFor(model => model.CompanyName)
</dd>
My model looks like this :
public class Company
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int CompanyId { get; set; }
public byte[] ImageData { get; set; }
[NotMapped]
public HttpPostedFileBase UploadImage { get; set; }
[NotMapped]
public string ImageBase64 => System.Convert.ToBase64String(ImageData);
public string CompanyName { get; set; }
public string CompanyAddress { get; set; }
public string CompanyCountry { get; set; }
public string CompanyCity { get; set; }
public string CompanyPostalCode { get; set; }
public string CompanyPhoneNumber { get; set; }
public string CAId { get; set; }
public virtual ICollection<WorkRole> WorkRoles { get; set; }
public virtual ICollection<UserDetails> UserDetails { get; set; }
}
And this is what I pass to the view from the controller :
Company company = db.Companies.Where(c => c.CAId == currentUserId)
.FirstOrDefault();
db.Users.FirstOrDefault(x => x.Id == currentUserId);
Any tips on how to achieve what I want?