namespace BoatShop.Controllers
{
public class ManagerController : Controller
{
ApplicationDbContext dbContext;
public ManagerController()
{
dbContext = new ApplicationDbContext();
}
public ManagerController(ApplicationUserManager userManager)
{
UserManager = userManager;
}
private ApplicationUserManager _userManager;
public ApplicationUserManager UserManager
{
get
{
return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
}
private set
{
_userManager = value;
}
}
public ActionResult Index()
{return View();}
public ActionResult ViewBoats()
{
var allBoats = dbContext.Boats.Where(x => x.isArchived == false).ToList();
return View(allBoats);
}
public ActionResult MakeOrder(int boatId)
{
OrderViewModel model = new OrderViewModel
{
boatId = boatId,
boatName = dbContext.Boats.FirstOrDefault(x => x.Id == boatId).Name,
ManagerName = User.Identity.Name,
userList = dbContext.Users.ToList()
};
return View(model);
}
[HttpPost]
public ActionResult MakeOrder(OrderViewModel model)
{
model.Order.Boat = dbContext.Boats.FirstOrDefault(x=>x.Id==model.boatId);
ApplicationUser seller = UserManager.FindById(User.Identity.GetUserId());
ApplicationUser customer = UserManager.FindById(model.CustomerId);
model.Order.SalePerson = seller;
model.Order.Customer = customer;
model.Order.CreationDate = DateTime.Now;
var order = model.Order;
dbContext.Orders.Add(order);
dbContext.SaveChanges();
return RedirectToAction("Index");
}
}
}
通过反复试验,我发现错误是由
model.Order.SalePerson = seller; model.Order.Customer = customer;
那些线。我想我需要以某种方式将它们从用户管理器中分离出来,但我不知道如何。我怎么解决这个问题?先感谢您。