做个有理想的人

ERP OA CMS SCM HR HIS

导航

MVC方式前台绑定数据替代Repeater中的无法使用if

Posted on 2014-11-21 10:35  爱国思想家  阅读(96)  评论(0)    收藏  举报

<div>
        方案1
        <%for (int i = 0; i < art_hot.Count; i++)
          {%>
        <%if (i == 0)
          {%>
        <td>
            ok<%=art_hot[i].Name%>
        </td>
        <%}
          else
          { %>
        <td>
            <%=art_hot[i].Name%>
        </td>
        <%} %>
        方案2
        <%} %>
        <%foreach (var r in art_hot)
          { %>
        <%if (r.Index == 0)
          { %>
        <td>
            ok<%=r.Name%>
        </td>
        <%}
          else
          { %>
        <td>
            <%=r.Name%>
        </td>
        <%} %>
        <%} %>
    </div>

 

 

public class Article
    {
        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private int index;

        public int Index
        {
            get { return index; }
            set { index = value; }
        }
    }
    public partial class _Default : System.Web.UI.Page
    {
        protected IList<Article> art_hot = null;
        protected void Page_Load(object sender, EventArgs e)
        {
            art_hot = new List<Article>();
            art_hot.Add(new Article() { Index = 0, Name = "1" });
            art_hot.Add(new Article() { Index = 1, Name = "1" });
            art_hot.Add(new Article() { Index = 2, Name = "1" });
            art_hot.Add(new Article() { Index = 3, Name = "1" });
            art_hot.Add(new Article() { Index = 4, Name = "1" });
        }
    }