如何搜索,并通过使用一个字符串关键字装载在数据库中的数据 [英] How to search and load data in database by using a string keyword

查看:146
本文介绍了如何搜索,并通过使用一个字符串关键字装载在数据库中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的 ProgramCategories 模式

public class ProgramCategories
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int id { get; set; }
    public string courseCategory { get; set; }
}

这是我的课程信息模型。

This is my course information model.

public class CourseCategory
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [Required(ErrorMessage = "Please Select Field of Study")]
    [Display(Name = "Field of Study")]
    public string courseField { get; set; }

    [Required(ErrorMessage = "Please Select Course Name")]
    [Display(Name = "Course Name", Prompt = "Course Name")]
    public string courseName { get; set; }

    [Display(Name = "Attachments")]
    public virtual ICollection<File> Files { get; set; }
    public virtual ICollection<FilePath> FilePaths { get; set; }
}

courseCategory ProgramCategories 是等于 courseField CourseCategory 。每当保存课程信息,我可以选择通过下拉课程的类别。下拉菜单包含了所有的 ProgramCategories 。我装的所有programCategories(例如:BankCourse,科学课程等)为索引视图。
所有这些东西都是工作的时刻。

The courseCategory in ProgramCategories is equals to courseField in CourseCategory. whenever saving course information i can select the category of course via dropdown. The dropdown contains all the ProgramCategories. I loaded all the programCategories(Ex:BankCourse, science course, etc) into index View. All these things are working for the moment.

我想知道当我点击一个 programCategory 我要显示所有该类别的课程。例如:如果我选择科学我当然要得到所有的理科课程。

What I want to know is when i click one programCategory i want to display all of the courses in that category. Ex: if i select science course i want to get all the science courses.

推荐答案

这是我会怎么对待它...

This is how I would approach it...

    public ActionResult CourseByCategory(string crsCtgry)
    {
       return View(getCourseByCategory(crsCtgry));
    }
    private List<CourseCategory> getCourseByCategory(string crsCtgry)
    {
        return db.CourseCategory.Where(n => n.courseField == crsCtgry).Select(n => n).ToList();
    }

这篇关于如何搜索,并通过使用一个字符串关键字装载在数据库中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆