我正在创建一个SchoolManagement
必须在其中创建StudentRegistration
表单的应用程序。在表单页面上,我必须显示Dropdownlist
也会来自数据库的内容。我是ASP MVC的新手,在ASP.NET中是如此简单,但是在MVC中我不知道该怎么做。我正在使用Ado.net
和Razor视图引擎。请有人指导为此需要哪些步骤。这是我的模特班
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; }
}
您需要创建操作方法,该方法将填充您的下拉菜单。您需要设置的列表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);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句