使用Automapper将viewmodel映射到域模型会在POST操作中返回空值吗?

时间:2015-07-04 01:45:55

标签: asp.net-mvc-5 ef-code-first automapper

我有一个记录一些员工信息的实体模型。我有一个类似的视图模型。

在我的创建操作中,我创建了一个viewmodel的新实例,并为下拉列表传递了几个列表。

在我的创建后期操作中,我使用AutoMapper将返回的视图模型映射到实体模型。

我做错了,因为回发的数据似乎不包含任何视图模型数据。它只包含空值。

任何想法我做错了什么?

控制器

largest = None
smallest = None
while ( True ) :
    inp = raw_input('Enter a number: ')
    if inp == 'done' :
        break
    try:
        inp = float(inp)
    except:
        print 'Invalid input'
    continue
    if inp is None or inp > largest:
        largest = inp
    if inp is None or inp < smallest:
        smallest = inp
print largest, smallest

视图

   // GET: /TimeCreditEntries/Create
    public ActionResult Create()
    {
        var Model = new TimeCreditEntryViewModel();  //Create a new viewmodel to hold the TimeCreditEntry
        Model.StationList = new SelectList(getStations(), "StationId", "Code");
        Model.AuthorisedEmployee = new SelectList(getEmployees(), "EmployeeId", "FullNameSurnameFirst");
        Model.AuthorisedPayment = new SelectList(getPaymentTypes(), "PaymentTypeId", "Code");
        return View(Model); //View is returned with model as parameter.
   }



    // POST: /TimeCreditEntries/Create
    // To protect from overposting attacks, please enable the specific properties you want to bind to, for 
    // more details see http://go.microsoft.com/fwlink/?LinkId=317598.
    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create([Bind(Include = "TimeCreditEntryId,RecordedDate,TimeCreditDate,ShiftId,StationId,AuthorisingEmployeeId,AuthorisedEmployeeId,ModifiedDate")] TimeCreditEntryViewModel timeCreditEntry)
    {
        if (ModelState.IsValid)
        {
            Mapper.CreateMap<TimeCreditEntryViewModel, TimeCreditEntry>();
            db.TimeCreditEntries.Add(Mapper.Map<TimeCreditEntryViewModel, TimeCreditEntry> (timeCreditEntry));
            db.SaveChanges();
            return RedirectToAction("Register"); }
                                             ViewBag.AuthorisedEmployeeId = new SelectList(db.Employees, "EmployeeId", "Title", timeCreditEntry.AuthorisedEmployeeId);
              ViewBag.AuthorisingEmployeeId = new SelectList(db.Employees, "EmployeeId", "Title", timeCreditEntry.AuthorisingEmployeeId);
              ViewBag.ShiftId = new SelectList(db.Shifts, "ShiftId", "Code", timeCreditEntry.ShiftId);
        return View(timeCreditEntry);
    }

0 个答案:

没有答案