好了,我已经看了一段时间才能找到问题的答案,但是似乎可以接受的答案对某些人有用,但对我来说不是。
下面是我当前的实现,以检查@Model.ListImages
是否为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);
}
}
模型
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; }
}
答案 0 :(得分:1)
问题是您正在混合使用JavaScript和Razor,应避免使用。如果可以做到仔细即可完成,但这会导致难以阅读,即难以调试和维护。
您的代码假定Razor将不会基于JavaScript评估执行。但是,事实并非如此,Razor语句将始终尝试求值。
// this looks like JavaScript with in-lined Razor.
// The Razor is executed before this JavaScript is executed.
var img = @((Model != null && Model.ListImages != null).ToString().ToLower());
// this is a JavaScript conditional based on a run-time value
if (img) {
// @Model.ListImages.Count() is evaluated by Razor BEFORE
// (and in a different context) the if(img) above.
// But there isn't a Razor condition preventing the execution of .Count()
// if ListImages is not initialized.
if (@Model.ListImages.Count() + this.files.length > 8) {
this.removeFile(file);
}
}
因此,您需要一个Razor条件才能输出不同的JavaScript代码。
@* Razor evaluated on the server while rendering the view *@
@if (Model != null && Model.ListImages != null)
{
var count = Model.ListImages.Count;
<script>
// JavaScript evaluated in the client
var images = @count; // "hardcoded" count on the server
if (images > 0)
{
removeFile()
}
</script>
}
一种避免混合的更干净的方法是将Razor值编码为HTML元素。
<div id="image"
data-count="@((Model != null && Model.ListImages != null) ? Model.ListImages.Count : 0)">
</div>
然后获取JavaScript中的值,该值无需动态输出不同的js代码即可运行。
var count = $("#image").data("count");
if (count > 0) {
removeFile();
}
答案 1 :(得分:0)
var length = "@(Model.ListImages.Count())";
if(length > 0){
// do something
}
答案 2 :(得分:0)
JavaScript有一种比较复杂的方法(如果您不知道的话)来检查真实值和虚假值的条件。当您像以前那样直接检查变量时
if (img)
它实际上并不会检查您提供的变量的值是false还是true,但是会存在该对象。因此,即使img
为假,您的代码也会进入该位置,并且您会得到null reference
异常。
尝试简单
if (img != false)
但是,正如另一个答案中所建议的那样,
if (@Model && @Model.ListImages && @Model.ListImages.Count() > 0)
更好