现在的位置: 首页 > 综合 > 正文

Razor例子

2013年01月21日 ⁄ 综合 ⁄ 共 2133字 ⁄ 字号 评论关闭
///
comment.cs
///
 
using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// Summary description for ClassName
/// </summary>
public class Comment
{
    public Comment()
    {
        //
        // TODO: Add constructor logic here
        //
    }
    public Guid Id { get; set; }
    public string Username { get; set; }
    public string Content { get; set; }
    public DateTime CreatedTime { get; set; }
    public Guid? ParentId { get; set; }
}
 
///
index.cshtml
///
 
@{
    if (IsPost)
    {
        var content = Request.Form["content"];
        var username = Request.Form["username"];
        var comment = new Comment
        {
            Content = content,
            Username = username,
            CreatedTime = DateTime.Now,
            Id = Guid.NewGuid()
        };
        if (Request.Form["parentid"] != "")
        {
            comment.ParentId = new Guid(Request.Form["parentid"]);
        }
        var list = Context.Application["list"] as List<Comment>;
        if (list == null)
        {
            list = new List<Comment>();
            Context.Application["list"] = list;
        }
        list.Add(comment);
        Response.Redirect("~/index.cshtml");
    }          
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>
    <ul>
        @{
            var allList = Context.Application["list"] as List<Comment> ?? new List<Comment>();
        }
        @foreach (var item in allList.Where(c => !c.ParentId.HasValue))
        {
            <li>内容:@item.Content<br />
                发布人:@item.Username@@1
                @item.CreatedTime.ToString("MM-dd HH:mm:ss")<br />
                <a href="?id=@item.Id">回复</a>
                <ul>
                    @foreach (var subitem in allList.Where(c => c.ParentId == item.Id))
                    {
                        <li>@subitem.Content|@item.Username@@|@item.CreatedTime.ToString("MM-dd HH:mm:ss")
                        </li>
                    }
                </ul>
            </li>
        }
    </ul>
    <form action="" method="post">
    <input type="hidden" name="parentid" value="@Request.QueryString["id"]" />
    <label>
        内容</label>
    <textarea name="content"></textarea><br />
    <label>
        用户名</label>
    <input type="text" name="username" />
    <input type="submit" name="提交" />
    </form>
</body>
</html>

抱歉!评论已关闭.