如何在asp.net razor视图中显示自定义错误

时间:2012-06-20 11:18:27

标签: asp.net razor webmatrix

如果数据库中不存在UserId,我想显示“找不到配置文件”消息。我该如何实现它。以下代码不起作用。

          UserId = Request.QueryString["UserId"];
           var db = Database.Open("mystring");
           var selectCommand = "SELECT * FROM UserProfile WHERE UserId = @0";
           var row = db.QuerySingle(selectCommand, UserId);

             if (row != null)
            {
                Email = row.Email;
                fullname = row.fullname;
                about = row.about;
             }else{
                 ModelState.AddFormError("No Profile Found.");

             }

1 个答案:

答案 0 :(得分:0)

试试这样:

@{
    var userId = Request.QueryString["UserId"];
    var db = Database.Open("mystring");
    var selectCommand = "SELECT * FROM UserProfile WHERE UserId = @0";
    var row = db.QuerySingle(selectCommand, UserId);
}

@if (row != null) {
    <div>
        Email: @row.Email
    </div>
    <div>
        Fullname: @row.fullname
    </div>
    <div>
        About: @row.about
    </div>
} else {
    <div>No Profile Found.</div>
}