1

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?

Darren
  • 68,902
  • 24
  • 138
  • 144
Robert Ross
  • 1,151
  • 2
  • 19
  • 47
  • Are you returning a model to the view - the only way the code you have shown could throw an exception is is `Model` is `null` –  Feb 11 '17 at 11:19
  • Yes, i am returning a model. – Robert Ross Feb 11 '17 at 11:25
  • Then you have not shown us the code which is really throwing the exception. The `@if (Model.ImageBase64 != null || Model.ImageData!= null )` is not even required –  Feb 11 '17 at 11:26
  • I though I showed you the code that is throwing it..What else it might be if not the view or the controller? The exception is thrown, because string ImageBase64 is not nullable and i want to bypass that. Excuse my noobnes. – Robert Ross Feb 11 '17 at 11:30
  • The issue is not the view - its `public string ImageBase64 => System.Convert.ToBase64String(ImageData);` - the `ToBase64String(null)` throws the exception –  Feb 11 '17 at 11:38
  • exactly, but i don't want to change this part. – Robert Ross Feb 11 '17 at 11:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/135442/discussion-between-stephen-muecke-and-robert-ross). –  Feb 11 '17 at 11:43

2 Answers2

1

You could perform a null check and ensure that the ImageData property is valid before rendering it in the View.

if (@model.ImageData?.Length > 0) { }

You could also take it one step further and modify your LINQ query to filter companies that have a valid image.

 db.Companies.Where(c => c.CAId == currentUserId
                    && c.ImageBase64 != null
                    && c.ImageData?.Length > 0).FirstOrDefault();

As an additional note, ensure you are passing a populated model back to your view correctly.

public ActionResult DisplayCompanyImages() 
{
    var model = // Retrieve db.Companies
    return View(model);
}
Darren
  • 68,902
  • 24
  • 138
  • 144
  • The exception is not because `ImageBase64` is `null` - its because `Model` is `null` –  Feb 11 '17 at 11:23
  • Thanks for the answer. I tried it, but it gave me the following exception : LINQ to Entities does not recognize the method 'Boolean IsNullOrWhiteSpace(System.String)' method, and this method cannot be translated into a store expression. – Robert Ross Feb 11 '17 at 11:24
  • @RobertRoss - I've amended my answer to show a LINQ to Entities replacement for `.IsNullOfWhiteSpace` – Darren Feb 11 '17 at 11:29
  • @StephenMuecke - OP hasn't provided any code showing what model they are returning to the View and stated they have a populated model. I have also included it in my answer as a safe check. – Darren Feb 11 '17 at 11:30
1

If you are using Database First, open your Model (*.edmx file), find the entity type that represents the database table, right-click on the image property and select Properties from the context menu. In the Properties window, change Nullable from False to True.

Peter Bill
  • 508
  • 3
  • 12