Asp.net mvc 4 Mvc:获取dropdownlistFor框的选定值
在我的领域,我有这个服务Asp.net mvc 4 Mvc:获取dropdownlistFor框的选定值,asp.net-mvc-4,Asp.net Mvc 4,在我的领域,我有这个服务 public class StudentService { private readonly IStudentRepository _studentRepository; public StudentService(IStudentRepository studentRepository) { _studentRepository = stu
public class StudentService
{
private readonly IStudentRepository _studentRepository;
public StudentService(IStudentRepository studentRepository)
{
_studentRepository = studentRepository;
}
public StudentDto DisplayStudentInformation()
{
var objStuSec = _studentRepository.DisplayStudentSection();
return objStuSec;
}
}
这是我的学生
public class StudentDto
{
public string StudentId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public List<DepartmentDto> GetAllDepartments;
}
这是我的表单html
@using System.Net.Mime
@model Zakota.University.Domain.DTO.StudentDto
<form action="" method="post">
<div>
<label>First Name</label>
@Html.TextBoxFor(x=>x.FirstName, new { id = "testid1", name="firstname" })
</div>
<div>
<label>Last Name</label>
@Html.TextBoxFor(x=>x.LastName, new { id = "testid1", name="lastname" })
</div>
<div>
<label>Email Address</label>
@Html.TextBoxFor(x=>x.EmailAddress, new { id = "testid1", name="emailaddress" })
</div>
<div>
<label>Department</label>
@Html.DropDownListFor( x=> x.GetAllDepartments,new SelectList(Model.GetAllDepartments,"DepartmentId","DepartmentDescription"), new {@class = "mydropdown", name="dept"})
</div>
<div>
<label></label>
<input type="submit" value="submit"/>
</div>
</form>
元素回发单个值。不能将
绑定到复杂对象的集合(在您的情况下是列表
)。首先创建一个表示要编辑内容的视图模型
public class StudentVM
{
[Display(Name = "First Name")]
[Required(ErrorMessage = "Please enter your first name")]
public string FirstName { get; set; }
.... // other properties of StudentDto
[Display(Name = "Department")]
[Required(ErrorMessage = "Please select a department")]
public int SelectedDepartment { get; set; }
public SelectList DepartmentList { get; set; }
}
接下来,您的StudentDto
模型不应包含包含所有部门集合的属性。您使用的DropDownListFor()
表明每个学生只有一个系,因此属性应为“public DepartmentDto>Department
控制器
public ActionResult Create()
{
StudentVM model = new StudentVM();
ConfigureCreateViewModel(model);
return View(model);
}
[HttpPost]
public ActionResult Create(StudentVM model)
{
if(!ModelState.IsValid)
{
ConfigureCreateViewModel(model); // reassign the select list
return View(model); // return the view so user can correct errors
}
StudentDto student = new StudentDto()
{
FirstName = model.FirstName,
LastName = model.LastName,
EmailAddress = mdoel.EmailAddress,
Department = db.Departments.Find(model.SelectedDepartment) // modify to suit
}
// save StudentDto and redirect
}
private void ConfigureCreateViewModel(StudentVM model)
{
List<DepartmentDto> departments = // call a service to get a collection of all departments
model.DepartmentList = new SelectList(departments, "DepartmentId","DepartmentDescription");
}
public ActionResult Create()
{
StudentVM模型=新建StudentVM();
配置CreateViewModel(模型);
返回视图(模型);
}
[HttpPost]
公共行动结果创建(StudentVM模型)
{
如果(!ModelState.IsValid)
{
ConfigureCreateViewModel(模型);//重新分配选择列表
返回视图(模型);//返回视图以便用户更正错误
}
StudentDto student=新学生dto()
{
FirstName=model.FirstName,
LastName=model.LastName,
EmailAddress=mdoel.EmailAddress,
Department=db.Departments.Find(model.SelectedDepartment)//修改以适应
}
//将StudentDto保存到并重定向
}
私有void配置CreateViewModel(StudentVM模型)
{
List departments=//调用服务以获取所有部门的集合
model.DepartmentList=新选择列表(部门,“部门ID”、“部门说明”);
}
看法
@model yoursassembly.StudentVM
@使用(Html.BeginForm())
{
@LabelFor(m=>m.FirstName)
@Html.TextBoxFor(m=>m.FirstName)
@Html.ValidationMessageFor(m=>m.FirstName)
..//StudentVM的其他控件
@Html.LabelFor(m=>m.SelectedDepartment)
@Html.DropDownListFor(m=>m.SelectedDepartment,Model.DepartmentList,“--请选择--”)
@Html.ValidationMessageFor(m=>m.SelectedDepartment)
}
旁注:
@Html.LabelFor(m=>m.FirstName)
而不是第一个
名称
。
是与控件关联的元素
(单击它将焦点设置为关联的控件)-您可以使用
不执行任何操作,因为它缺少属性的
id
和
名称
属性。在您的情况下,您正在生成无效的html
创建重复的id
属性(前3个元素
id=“testid1”
)并且您不应尝试设置名称
属性(在前3种情况下,您只需将其设置为
反正已经是了,但是在下拉列表的情况下,您试图
将其更改为name=“dept”
,幸运的是它不起作用-因为
如果这样做,绑定将失败!)[必需(ErrorMessage=“请输入第一个
名称“)]
并在视图中包括@Html.ValidationMessageFor(m=>
m、 名字)
谢谢。我跟随你的脚步,一切看起来都很好。我现在遇到的唯一问题是,当我发布文章时,单击“创建”按钮时,我得到的DepartmentList值为空。如果返回视图,则需要重新指定DepartmentList的值(仅当ModelState无效时才应执行此操作)。通常,最好使用
private void ConfigureViewModel(StudentVM model)
方法来设置在GET方法中调用的selectlist
(和其他常用值),如果返回视图,请再次在POST方法中调用。请向我展示如何执行此操作的代码示例?谢谢,非常感谢。我的意思是,当我在方法public ActionResult Index(StudentViewModel model)上设置断点时,DepartmentList=Null。它总是空的,这意味着我没有从dropdownlist中选择值。但是我可以获取部门ID。DepartmentList
将始终为空。您不会(也不应该)为SelectList
的每个属性生成任何控件。所选选项的DepartmentId
绑定到SelectedDepartment
属性。
public class StudentVM
{
[Display(Name = "First Name")]
[Required(ErrorMessage = "Please enter your first name")]
public string FirstName { get; set; }
.... // other properties of StudentDto
[Display(Name = "Department")]
[Required(ErrorMessage = "Please select a department")]
public int SelectedDepartment { get; set; }
public SelectList DepartmentList { get; set; }
}
public ActionResult Create()
{
StudentVM model = new StudentVM();
ConfigureCreateViewModel(model);
return View(model);
}
[HttpPost]
public ActionResult Create(StudentVM model)
{
if(!ModelState.IsValid)
{
ConfigureCreateViewModel(model); // reassign the select list
return View(model); // return the view so user can correct errors
}
StudentDto student = new StudentDto()
{
FirstName = model.FirstName,
LastName = model.LastName,
EmailAddress = mdoel.EmailAddress,
Department = db.Departments.Find(model.SelectedDepartment) // modify to suit
}
// save StudentDto and redirect
}
private void ConfigureCreateViewModel(StudentVM model)
{
List<DepartmentDto> departments = // call a service to get a collection of all departments
model.DepartmentList = new SelectList(departments, "DepartmentId","DepartmentDescription");
}
@model yourAssembly.StudentVM
@using (Html.BeginForm())
{
@Html.LabelFor(m => m.FirstName)
@Html.TextBoxFor(m => m.FirstName)
@Html.ValidationMessageFor(m => m.FirstName)
.... // other controls of StudentVM
@Html.LabelFor(m => m.SelectedDepartment)
@Html.DropDownListFor(m => m.SelectedDepartment, Model.DepartmentList, "--Please select--")
@Html.ValidationMessageFor(m => m.SelectedDepartment)
<input type="submit" value="submit"/>
}