How to search and load data in database by using a string keyword

天涯浪子 提交于 2020-01-17 14:35:09

问题


This is my ProgramCategories model

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; }
}

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.

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.


回答1:


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();
    }



回答2:


What you're describing is a one-to-many relationship, so handle it as such. Create a true foreign key between the two tables and then you can automatically pull in the course that belong to a category via either eager or lazy loading in Entity Framework:

public class ProgramCategories
{
    ...

    public virtual ICollection<CourseCategory> Courses { get; set; }

}

public class CourseCategory
{
    ...

    [ForeignKey("Category")]
    public int CategoryId { get; set; }
    public virtual ProgramCategories Category { get; set; }

}

Here I've used the primary key of ProgramCategories as the foreign key. You don't technically have to do it that way. You could still use a string as you're doing with courseField; you would just need to define a max length for the property:

[ForeignKey("Category")]
[MaxLength(50)]
public string CourseField { get; set; }

Otherwise, the property will be represented as an NVARCHAR(MAX) in the database, which can't be indexed (it's essentially the same as TEXT). However, best practice is to key off the primary key.

Also, your class naming is horrendous. Classes should be singular ProgramCategory vs. ProgramCategories, and your CourseCategory name doesn't actually describe what this class is. Seems Course would be more appropriate, as it looks like that's what you're actually defining here, but maybe courseName is another should-be-a-foreign-key scenario. When you name your classes and properties on those classes, you should be thinking about how you would read it. For example, "The course category course name is 'Foo'." makes no sense, but "The course name is 'Foo'." makes perfect sense. That would translate to something like:

public class Course
{
    public string Name { get; set; }
}


来源:https://stackoverflow.com/questions/31835598/how-to-search-and-load-data-in-database-by-using-a-string-keyword

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!