Ok I have looked a while for some answers to my question but seems like the accepted answers works for some but for me isn't.
Below is my current implementation to check if @Model.ListImages is not null
var img = @((Model != null && Model.ListImages != null).ToString().ToLower());
if (img) {
if (@Model.ListImages.Count() + this.files.length > 8) {
this.removeFile(file);
$('#errorMessage').html("Cannot upload more then 8 images for a product.");
$("#errorDiv").show();
window.scrollTo(0, 0);
}
}
The Model
public class ProductVM
{
[Required]
public int Id { get; set; }
[Required]
[Display(Name = "Category")]
public int IdCategory { get; set; }
[Required]
[Display(Name = "Collection")]
public int IdCollection { get; set; }
[Required]
[Range(0.1, 20000, ErrorMessage = "Please enter a value between 0 and 20000")]
public decimal Price { get; set; }
[Required]
[Display(Name = "Product Name")]
[StringLength(100, ErrorMessage = "Product name must contain at least {2} characters", MinimumLength = 6)]
public string Name { get; set; }
[Required]
[Display(Name = "Product Description")]
[StringLength(500, ErrorMessage = "Product name must contain at least {2} characters", MinimumLength = 25)]
public string Description { get; set; }
public HttpPostedFileBase[] Listfiles { get; set; }
public virtual IEnumerable<SelectListItem> Categories { get; set; }
public virtual IEnumerable<SelectListItem> Collections { get; set; }
public virtual IEnumerable<Reviews> ListReviews { get; set; }
public virtual ICollection<Images> ListImages { get; set; }
}
@Model.ListImages.Count- this will throw the exceptionValue cannot be null// some codeis throwing an exception you'll need to share that with us along with a definition for your Model.