我修改了这个 ContosoUniversity 教程: http ://www.asp.net/mvc/tutorials/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an- asp-net-mvc-application现在我正在尝试使用 AutoMapper 将对象映射到对象。
这是我的模型视图:
public class InstructorIndexData
{
public IEnumerable<Instructor> Instructors { get; set; }
public IEnumerable<Course> Courses { get; set; }
public IEnumerable<Enrollment> Enrollments { get; set; }
public IEnumerable<Student> Students { get; set; }
public IEnumerable<Assignment> Assignments { get; set; }
}
这是视图
@model SchoolIn.ViewModels.InstructorIndexData
@using SchoolIn.Models
@{ViewBag.Title = "Index";}
@using (Html.BeginForm("ClassAttendance", "Attendance",
new { currentDate = day, id = @ViewBag.ID,
teacher = HttpContext.Current.Session["sTeacher"],
courseID = HttpContext.Current.Session["sCourseID"] }, FormMethod.Post))
{
var wekdys = new Enrollment(); @*had too create this to have access to the dropdownList*@
@Html.DropDownList("weekDays", wekdys.WeekDays.Select(s =>
new SelectListItem { Text = s.ToString(), Value = s.ToString() }))
@Html.ValidationSummary(true)
<p></p>
<Input Type ="submit" Value="ClassAttendance"/>
<p>@Html.ActionLink("Create Todays Attendance", "Create")</p>
<p>@Html.ActionLink("Create New", "Create")</p>
<h3>Students Enrolled in @ViewBag.teacherName's @ViewBag.courseTitle Course</h3>
<table>
<tr>
<th>View Student Report</th>
<th>First Name</th>
<th>Last Name</th>
<th>Grade</th>
<th>Attendance Code</th>
<th>Class Day</th>
</tr>
@foreach (var item in Model.Enrollments)
{
<tr>
<td>
@Html.ActionLink("Report", "UpdateAttendance", "Attendance",
new {
grade=item.Grade,
studentFirstName = item.Student.FirstMidName,
studentLastName = item.Student.LastName,
courseTitle = item.Course.Title,
teacher = @ViewBag.teacherName }, null)
</td>
<td>@Html.EditorFor(modelItem=>item.Student.FirstMidName)</td>
<td>@Html.EditorFor(modelItem=>item.Student.LastName)</td>
<td>@Html.EditorFor(modelItem =>item.Grade)</td>
<td>@Html.DropDownList("attendanceCode", item.attendanceCode.Select(s =>
new SelectListItem { Text = s.ToString(), Value = s.ToString() }))
@*Html.EditorFor(modelItem =>item.attendanceCode)*/*@
</td>
<td>@Html.EditorFor(modelItem=>item.classDays)</td>
</tr>
}
</table>
}
这是我的控制器中遇到问题的方法。我正在尝试使用这个例子:http ://chriscurrie.co.uk/blog/2011/09/coding/net/mvc/use-automapper-to-map-viewmodels-to-entities-mvc3-and-razor /
public class AttendanceController : Controller
{
private SchoolInDB db = new SchoolInDB();
}
代码在这里...</p>
[HttpPost]
public ActionResult ClassAttendance(InstructorIndexData viewModel)
{
if (ModelState.IsValid)
{
AutoMapper.Mapper.CreateMap<AttendanceData, Assignment>();
AutoMapper.Mapper.CreateMap<AttendanceData, Enrollment>();
AutoMapper.Mapper.CreateMap<AttendanceData, Student>();
AutoMapper.Mapper.CreateMap<AttendanceData, Instructor>();
Instructor newInstructor = new Instructor();
Student newStudent = new Student();
Assignment newAssignment = new Assignment();
Enrollment newEnrollment = new Enrollment();
AutoMapper.Mapper.Map(viewModel, newInstructor );
AutoMapper.Mapper.Map(viewModel, newStudent);
AutoMapper.Mapper.Map(viewModel, newEnrollment);
AutoMapper.Mapper.Map(viewModel, newAssignment);
db.Assignments.AddObject(newAssignment).;
db.SaveChanges();
return RedirectToAction("Index");
}
return View();
}
当我调试我的代码在这里停止'AutoMapper.Mapper.Map(viewModel, newInstructor);' 出现此错误:缺少类型映射配置或不支持的映射。
映射类型:InstructorIndexData -> Instructor SchoolIn.ViewModels.InstructorIndexData -> SchoolIn.Models.Instructor
目的地路径:导师
源值:SchoolIn.ViewModels.InstructorIndexData
我还注意到,在我的 ActionResult(InstructorIndexData viewModel) 中,viewModel 具有所有空值。