开发者

Razor MVC partial view cannot find end of code blocks and LI tags

I have a partial view that is not rendering:

The designer is telling me the foreach(Dress d in Model) is missing a closing character "}" the foreach(Picture p in d.Pictures) is missing a closing character "}" and the LI item is not closed

@model IEnumerable<MyApp.Model.Dress>

        <ul class="thumbs noscript">
            @{                                                              
                foreach (Dress d in Model)
                { 
                    foreach(Picture p in d.Pictures)      
                    {                      
                        string dressPrefix = "images/Dresses/"+p.Dress.DressName;
                        string bigThumb = dressPrefix+"-"+p.Caption+"_bigthumb.jpg";
                        string thumb = dressPrefix+"-"+p.Caption+"_thumb.jpg";
                        string dressTitle = d.DressName+" "+d.Price.ToString();

                    <li>
                        <a class="thumb" href="@bigThumb" title="@dressTitle">
                            <img src="@thumb" alt=""@dressTitle" />
                        </a>
                    </li>                                                        
                    }开发者_开发百科
                }
            }
        </ul>

 public class Dress
    {
        public int ID { get; set; }
        public int GalleryTypeID { get; set; }
        public virtual GalleryType GalleryType { get; set; }
        public int DressTypeID { get; set; }
        public virtual DressType DressType { get; set; }
        public int DesignerID { get; set; }
        public virtual Designer Designer { get; set; }
        public string Description { get; set; }
        public string DressName { get; set; }
        public string Bust { get; set; }
        public int DressLength { get; set; }
        public int SleeveLength { get; set; }
        public decimal Price { get; set; }
        public int Waist { get; set; }
        public int test { get; set; }

        public virtual ICollection<DressColor> DressColors { get; set; }
        public virtual ICollection<Picture> Pictures { get; set; }

    }

public class Picture
{
    public int ID { get; set; }
    public Dress Dress { get; set; }
    public string Caption { get; set; }
    public string PictureURL { get; set; }
}

Any ideas?


Take out hte extra braces, also you have an extra quote in the alt attribute of the img tag, which is probably what is causing your unclosed tag problem.

@model IEnumerable<MyApp.Model.Dress>

<ul class="thumbs noscript">
@foreach (Dress d in Model) { 
    foreach(Picture p in d.Pictures) {                      
        string dressPrefix = "images/Dresses/"+p.Dress.DressName;
        string bigThumb = dressPrefix+"-"+p.Caption+"_bigthumb.jpg";
        string thumb = dressPrefix+"-"+p.Caption+"_thumb.jpg";
        string dressTitle = d.DressName+" "+d.Price.ToString();

        <li>
            <a class="thumb" href="@bigThumb" title="@dressTitle">
                <img src="@thumb" alt="@dressTitle" />
            </a>
        </li>                                                        
    }
}
</ul>

FYI, you really shouldn't be doing that much code in your view, that should be in your controller. You should also use string.Format to format the strings, not using the + operator.


It looks like your newly created variables are not being recognized by razor. You could just swap the variables by their expressions inside your li tag such as: title="@(d.DressName+" "+d.Price.ToString())".

Alternatively, you can take out the braces outside your foreach.

I would instead prefer creating a View Model to hold your prefix string and just using them directly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜