WebMatrix轻量级web开发Razor的一个小demo

@{
    if(IsPost)
    {
        var contentx = Request.Form["contentx"];
        var userName = Request.Form["username"];
        var conment = new Content {
            content = contentx,
        userName=userName,
        addTime=DateTime.Now,
        guid=Guid.NewGuid()
        };
        if (Request.Form["parentid"] != "")
        {
            conment.pid = new Guid(Request.Form["parentid"]);
        }
       
    var list = Context.Application["list"] as List<Content>;
    if(list==null)
    {
        list = new List<Content>();
        Context.Application["list"] = list;
    }
      list.Add(conment);
      Response.Redirect("Index.cshtml");
    }
  
   
   
 
   
    }


<!DOCTYPE html>

<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <ul>
        @{
            var allist=(Context.Application["list"] as List<Content>)??new List<Content>();
            }
          
                @foreach(var item in allist)
                {
                    <li>
                    内容:@item.content<br />
                    发布人:@item.userName @@
                    @item.addTime.ToString("MM:dd: HH:mm:ss") <br />
                    <a href="?id=@item.guid">回复</a>
                    </li>
                    if (item.pid != null)
                    {
                        <ul>
                         @foreach(var it in allist.Where(r=>r.pid==item.guid))
                         {
                         <li>
                          @it.content
                         </li>
                         }
                        </ul>
                    }
               
                }
           
       
       
        </ul>

        <form action="" method="post">
        <input type="hidden" value="@Request.QueryString["id"]" name="parentid" />
        <label title="">留言内容:</label><textarea name="contentx"></textarea>
        <label title="">用户名</label><input type="text" name="username" />
        <input type="submit" value="提交" />

       
        </form>
    </body>
</html>

 

对象:Content

 

public class Content
{
    public Content()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public Guid guid { get; set; }
    public string userName { get; set; }
    public string content { get; set; }
    public DateTime addTime { get; set; }
    public Guid? pid { get; set; }

   
}

 

posted @ 2011-05-22 22:00  风云8  阅读(348)  评论(0编辑  收藏  举报