我修改了this ContosoUniversity tutorial,现在我正在尝试使用AutoMapper来映射对象到对象。
这是我的ModelView:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using SchoolIn.Models;
namespace SchoolIn.ViewModels
{
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>
}
这是我控制器中遇到问题的方法。我正在尝试使用this example:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Objects;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using SchoolIn.Models;
using SchoolIn.ViewModels;
namespace SchoolIn.Controllers
{
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();
}
当我尝试db.Assignment.AddObject(newAssignment);
时,AddObject
方法无法使用。
我用错了吗?为什么AddObject
没有AddObject()
可用?