0

Model Class:

public class Course
{
    [Key]
    public int Id { get; set; }

    [MinLength(5, ErrorMessage = "Code must be (5) characters long")]
    [Index(IsUnique = true)]
    [Column(TypeName = "VARCHAR")]
    [Required]
    [Display(Name = "Code")]
    public string CourseCode { get; set; }

    [Index(IsUnique = true)]
    [Column(TypeName = "VARCHAR")]
    [Required]
    [Display(Name = "Name")]
    [Remote("IsCourseNameExist", "Courses", HttpMethod = "POST", ErrorMessage = "Course is existed.")]
    public string CourseName { get; set; }

    //[System.ComponentModel.DataAnnotations.Compare("CourseName", ErrorMessage = "Already this Course is exist.")]
    //[NotMapped]
    //public string VeryfyName { get; set; }

    [Range(0.5, 5, ErrorMessage = "Credit Must be between (0.5) to (5.0)")]
    [Display(Name = "Credit")]
    public decimal CourseCredit { get; set; }

    public string Description { get; set; }
    public int DepartmentId { get; set; }
    public int SemesterId { get; set; }

    [ForeignKey("DepartmentId")]
    public virtual Department Department { get; set; }
    [ForeignKey("SemesterId")]
    public virtual Semester Semester { get; set; }

}

In Controller Class:

public JsonResult IsCourseNameExist(string CourseName)
    {
        //var course = .....
        return Json(course == null);
    }

In View:

<div class="form-group">
        @Html.LabelFor(model => model.CourseName, htmlAttributes: new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.CourseName, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.CourseName, "", new { @class = "text-danger" })
        </div>
    </div>

Description: I am using Code First EF and very new in this area. I have a Model class "Course" which generated a DB table named "Courses". In view, user will add courses for a selected department and semester. But the Course Name property is unique.If user give a course name already exist in "Courses" table and submit button, then some error generates. That's why I want to make confirm that the user can not put any existing course name. So it needs checking before submission.

I have searched a lot, as I am newbie, everything is not clear to me. I found a way to use [Remote()] in model class and then use an action in controller to solve this. But cant apply it.

I expect some brief how to write the code that I mentioned in Controller class and what additional things need to add in view.

Thanks!

  • Is this for the `Create` action? If so, please post that code. – Grizzly Feb 09 '17 at 17:29
  • Checking for an existing Course would be done at the server. Query for the course name and if you get a result do not insert. http://stackoverflow.com/questions/6966207/entityframework-insert-if-not-exist-otherwise-update – Jasen Feb 09 '17 at 17:32

1 Answers1

1

You can use a conditional statement combined with .Any() lambda expression.

public JsonResult IsCourseNameExist(string CourseName)
{
    if(dbContext.Courses.Any(x => x.CourseName.Trim().ToUpper().Equals(CourseName.Trim().ToUpper())
    {
        return Json(false);
    }
    else
    {
        return Json(true);
    }
}

Using .ToUpper() will help this be more efficient, because if your table you have a course name called Math 101.. and the user types in math 101 that might be submitted without error.

Let me know if this helps.

Grizzly
  • 5,873
  • 8
  • 56
  • 109
  • @MdNurulAfsarPervez Please mark this answer as accepted so that this question can be seen as answered. – Grizzly Feb 10 '17 at 12:50