How to check model string property for null in a razor view

asp.net-mvc-4 c# entity-framework razor

Question

I am creating anASP.NET MVC 4 application. I utilizeEF 5 I have, starting with code, in one of my entities:

public string ImageName { get; set; }
public string ImageGUIDName { get; set; }

are two things that belong to my entity. These values may be null since I may not have uploaded a picture, yet when I display the view, I send the model withImageName and ImageGUIDName emerging asnull I got the following from the database:

Exception Details: System.ArgumentNullException: Value cannot be null.

The primary concept is to give the user different text depending on whether or not there is a picture:

            @if (Model.ImageName != null)
            {
                <label for="Image">Change picture</label>
            }
            else
            { 
                <label for="Image">Add picture</label>
            }

I thus tried using when the above code gave me the problem.string.IsNullOrEmpty(Model.ImageName) both alsoModel.ImageName.DefaultIfEmpty() != null but I experienced the same issue. It seems impossible for me to just set the y entity property to nullable:

public string? ImageName { get; set; } //Not working as it seems

So how do I handle this?

1
29
12/5/2013 7:13:15 PM

Accepted Answer

ZZZ_tmp
64
12/5/2013 7:16:38 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow