1

SchoolManagementアプリケーションを作成中です。StudentRegistrationフォームを作成する必要があります。フォームページで私はDropdownlistも表示する必要がありますが、これもデータベースから来ます。私はasp.netで非常に簡単でしたが、MVCで私はそれを行う方法がわかりません。私はAdo.netとRazorビューエンジンを使用しています。誰かがそれに必要なステップを案内してください。 これは、あなたのドロップダウンを記入しますアクションメソッドを作成する必要がASP MVCアクセスデータベースからのドロップダウンリスト

public class StudentModel 
{ 
    [Required(ErrorMessage="Student ID is required", AllowEmptyStrings=false)] 
    public int StudentID { get; set; } 
    [Required(ErrorMessage = "Student Roll No is required", AllowEmptyStrings = false)] 
    public int RollNo { get; set; } 
    [Required(ErrorMessage = "Student Name is required", AllowEmptyStrings = false)] 
    public string StudentName { get; set; } 
    [Required(ErrorMessage = "Student Address is required", AllowEmptyStrings = false)] 
    public string Address { get; set; } 
    [Required(ErrorMessage = "Student Cell is required", AllowEmptyStrings = false)] 
    public string Cell { get; set; } 


} 

答えて

0

私のモデルクラスです。 SelectedListItemのリストを設定する必要があります。 エンティティフレームワークを使って説明してください。

あなたの方法は次のようになります。ビューで

public ActionResult Register() 
{ 
    var model = new RegistrationViewModel() 
    { 
     StudentsList = this.context.Students.Select(s => new SelectListItem 
     { 
      Text = s.Name, 
      Value = s.Id.ToString() 
     }, 
    // some other property to the model 
    } 

    return View(model); 
} 

//SelectedStudent - string property in your model, where will be stored Value from list 

@Html.DropDownListFor(model => model.SelectedStudent, Model.StudentsList) 

とPOSTメソッドで

[HttpPost] 
public ActionResult Register(RegistrationViewModel model) 
{ 
    var studentId = int.Parse(model.SelectedStudent); 
}