2017-02-09 5 views
0

モデルクラス:コントローラクラスでユーザーが入力している間に一意のプロパティを検証する方法(.NETの最初のアプローチ、MVC5)?

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

} 

:ビューで

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

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

説明:私はこの分野でのコードファーストEFと非常に新しいを使用しています 。 "Course"という名前のDBテーブルを生成するModelクラス "Course"があります。 ビューでは、選択した部門と学期のコースを追加します。しかし、Course Nameプロパティはユニークです。ユーザーが "Courses"テーブルに既に存在するコース名を指定し、Submitボタンを押すと、何らかのエラーが生成されます。だから私は、ユーザーが既存のコース名を入力できないことを確認したいのです。したがって、提出する前にチェックする必要があります。

私は初心者のように多くを検索しましたが、すべてが私には分かりません。モデルクラスで[Remote()]を使用し、コントローラでアクションを使用してこれを解決する方法が見つかりました。しかし、それを適用することはできません。

私はControllerクラスで言及したコードを書く方法と、追加で追加する必要があるものをいくつか見ています。

ありがとうございます!

+0

これは 'Create'アクション用ですか?もしそうなら、そのコードを投稿してください。 –

+0

既存のコースをチェックすることはサーバーで行われます。コース名を照会し、結果が得られた場合は挿入しないでください。 http://stackoverflow.com/questions/6966207/entityframework-insert-if-not-exist-otherwise-update – Jasen

答えて

1

ラムダ式.Any()と組み合わせた条件文を使用できます。

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

あなたのテーブル場合はMath 101と呼ばれるコース名...と、エラーなしで提出される可能性がありますmath 101でユーザータイプを持っているので、これは、より効率的にするのに役立ちます.ToUpper()を使用します。

これが役立つかどうか教えてください。

+0

ありがとう!それは働いている。 –

+0

@MdNurulAfsarPervezこの質問に回答があるように、この回答に合格とマークしてください。 –