我修改了这个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映射对象到对象。
这是我的ModelView:
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();
}
代码在这里......
[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 - &gt;讲师 SchoolIn.ViewModels.InstructorIndexData - &gt; SchoolIn.Models.Instructor
目的地路径: 讲师
来源价值: SchoolIn.ViewModels.InstructorIndexData
任何iI也注意到在我的ActionResult(InstructorIndexData viewModel)中,viewModel具有所有空值。
答案 0 :(得分:2)
根据我的理解,viewModel的类型是InstructorIndexData。如果是这样,您需要添加配置:
AutoMapper.Mapper.CreateMap<InstructorIndexData, Assignment>();
AutoMapper.Mapper.CreateMap<InstructorIndexData, Enrollment>();
AutoMapper.Mapper.CreateMap<InstructorIndexData, Student>();
AutoMapper.Mapper.CreateMap<InstructorIndexData, Instructor>();