この 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();
}
コードはこちら…</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 にすべて null 値があることに気付きました。