使用Viewbag在屏幕上查看搜索查询

时间:2015-06-26 15:03:24

标签: c# asp.net-mvc visual-studio razor view

我有这个视图,用户搜索特定的知识库文章,在结果视图的顶部,我想向用户显示他们的搜索条件是什么。所以我正在使用viewbag,但它是空白的。

这是我的控制器代码:

public ActionResult UserSearchKnowledgebase(SearchKnowledgebase searchKnowledgebase)
        {


            var model = db.Knowledgebases.AsQueryable();
            if (searchKnowledgebase.CreatedById != Guid.Empty)
            {
                model = model.Where(k => k.CreatedById == searchKnowledgebase.CreatedById);
                ViewBag.Createdby = searchKnowledgebase.CreatedBy.FullName;
            }
            if (searchKnowledgebase.CategoryId != Guid.Empty)
            {
                model = model.Where(k => k.CategoryId == searchKnowledgebase.CategoryId);
                ViewBag.Category = searchKnowledgebase.Category.CategoryName;
            }
            model = model.OrderBy(k => k.CreatedDate);
            var result = model.ToList();

            return View("UserKnowledgebaseList", result);

        }

这是我的观看代码:

<h3>Knowledgebase: Category: @ViewBag.Category      Created by: @ViewBag.Createdby</h3>

这是我的ViewModel代码:

namespace HelpDesk.WebUI.ViewModel
{
    public class SearchKnowledgebase
    {

        [Display(Name = "Category")]
        public Guid CategoryId { get; set; }

        public virtual Category Category { get; set; }

        [Display(Name = "Created By")]
        public Guid CreatedById { get; set; }

        public virtual User CreatedBy { get; set; }

        [Display(Name = "Type")]
        public Guid TypeId { get; set; }

        public virtual Role Type { get; set; }

    }
}

我以为我想出来并尝试了这一点,但仍然一无所获:

ViewBag.Category = db.Categories.Where(c => c.CategoryId == searchKnowledgebase.CategoryId).First().CategoryName;

1 个答案:

答案 0 :(得分:0)

抱歉,不能发表评论,所以我必须发布答案...你确定CreatedByID和CategoryID有值吗?也许它完全跳过那段代码?如果您可以看到是否可以在该特定行上设置断点