I am getting this error message on the Error List, but when I look up the code, I get no error highlight (red squiggly line), so there's no tips on how to fix this. The error is "An object reference is required for the non-static field, method, or property 'Mapper.Map<List<User>, List<UserViewModel>>(List<User>)'".
I don't understand the code fully (I didn't write it, I'm just making somes changes), but for what I know, it uses MVC, but it uses AutoMapper instead of Entity Framework.
The code is the following:
using System;
using System.Collections.Generic;
using System.Web.Mvc;
using System.Web.Security;
using App.Infra;
using App.Model;
using AutoMapper;
using Domain.Entities;
using Domain.IApp;
using Domain.ValueObject;
using System.Globalization;
namespace Web.Controllers
{
public class LoginController : BaseController
{
// GET: Login
private readonly IUserApp _userApp;
private readonly IFunctionApp _functionApp;
private readonly IChannelApp _channelApp;
public LoginController(IUserApp userApp, IFunctionApp functionApp, IChannelApp channelApp)
{
_userApp = userApp;
_functionApp = functionApp;
_channelApp = channelApp;
}
public ActionResult LogOn()
{
return View();
}
[HttpPost]
public ActionResult LogOn(LoginViewModel login)
{
var loginAd = ValidateLogin.ActiveDirectoryAuthenticate(login.User, login.Password);
User userAd = null;
if (loginAd != null)
{
userAd = _userApp.GetUserBySId(loginAd.AdUserId);
if (userAd == null)
{
var u = new UserViewModel()
{
AdUserId = loginAd.AdUserId,
Name = loginAd.Name,
Mail = loginAd.Mail,
DtInclude = DateTime.Now,
Perfil = loginAd.Perfil,
Login = login.User
};
var user = Mapper.Map<UserViewModel, User>(u);
_userApp.Add(user);
var function = Mapper.Map<FunctionViewModel, Function>(ValidateLogin.Permissions(user.Id, user.Perfil));
_functionApp.Add(function);
}
else
{
var functionByUserId = _functionApp.GetByUserId(userAd.Id);
if (functionByUserId != null)
{
var perfil = ValidateLogin.ValidateGroup(login.User, "Bandeirantes");
// var perfil = Perfil.APP_REC_ADMIN_PORTAL;
bool updatePerfil = false;
if (userAd.DtUpdate < Convert.ToDateTime("05/11/2016 10:29:27", new CultureInfo("pt-BR")))
{
updatePerfil = true;
}
if (perfil != userAd.Perfil || updatePerfil)
{
userAd.Perfil = (Perfil)perfil;
var functionValidate = _functionApp.GetByUserId(userAd.Id);
IList<int> list = new List<int>();
foreach (var item in functionValidate.Channels)
{
list.Add(item.Id);
}
foreach (var item in list)
{
_functionApp.RemoveRelation(functionValidate.Id, item);
}
var function = ValidateLogin.Permissions(userAd.Id, userAd.Perfil);
foreach (var item in function.Channels)
{
_functionApp.UpdateRelation(functionValidate.Id, item.Id);
}
}
else
{
if (userAd.Perfil == Perfil.APP_REC_ADMIN_PORTAL || userAd.Perfil == Perfil.APP_REC_PORTAL)
{
var functionValidate = _functionApp.GetByUserId(userAd.Id);
functionValidate.PublishYT = true;
functionValidate.PublishUOL = true;
_functionApp.Update(functionValidate);
}
}
}
else if (functionByUserId == null)
{
var function = ValidateLogin.Permissions(userAd.Id, userAd.Perfil);
var domain = new Function();
domain.PublishUOL = function.PublishUOL;
domain.PublishYT = function.PublishYT;
domain.DownloadVideo = function.DownloadVideo;
domain.UserId = userAd.Id;
domain.DtInclude = DateTime.Now;
_functionApp.Add(domain);
foreach (var item in function.Channels)
{
_functionApp.UpdateRelation(domain.Id, item.Id);
}
}
userAd.DtUpdate = DateTime.Now;
_userApp.Update(userAd);
}
FormsAuthentication.SetAuthCookie(loginAd.Name + "|" + loginAd.AdUserId + "|" + userAd.Perfil + "|" + loginAd.Mail.Adress, true);
return RedirectToAction("Index", "Home");
}
Danger("Usuário ou senha incorreto!");
return View();
}
public ActionResult LogOff()
{
FormsAuthentication.SignOut();
return View("LogOn");
}
}
}