我正在从视图中对控制器执行一个简单的ajax调用,以获得更改的文本值的下拉列表。
在控制器上,我使用存储库模式获取与该id关联的客户端列表。我的控制器代码如下所示。
public class CallCenterController : Controller
{
private readonly IUnitOfWork _unitOfWork;
private readonly ISubCompanyService _repoCompanyService;
public CallCenterController(IUnitOfWork unitOfWork, ISubCompanyService repoCompanyService)
{
_unitOfWork = unitOfWork;
_repoCompanyService = repoCompanyService;
}
[HttpPost]
public ActionResult GetLocationsByClient(int? clientID = 0)
{
try
{
List<SelectListItem> locs = new List<SelectListItem>();
IEnumerable<SelectListItem> locations = null;
var client = _repoCompanyService.GetCompanyListByClientID(clientID);
if (client != null)
{
locations = _repoLocationService.GetActiveLocationByCompanyID(client.sub_company_id).ToList().Select(
s => new SelectListItem
{
Text = s.name + " - " + s.address1 + " - " + s.city + " - " + s.state,
Value = s.location_id.ToString()
});
locs.Add(new SelectListItem { Text = "--Select--", Value = "0" });
foreach (var loc in locations)
{
locs.Add(loc);
}
return Json(new SelectList(locs, "Value", "Text"));
}
}
catch (Exception ex)
{
Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
}
return Json(new SelectList("", "Value", "Text"));
}
}
在服务中,我正在这样做
public class SubCompanyService : ISubCompanyService
{
private IUnitOfWork _unitOfWork;
private GenericRepository<sub_company> _reposubCompany;
public SubCompanyService(
IUnitOfWork unitOfWork,
GenericRepository<sub_company> reposubCompany)
public sub_company GetCompanyListByClientID(int? ClientID)
{
return _reposubCompany.Get(x => x.client_id == ClientID);
}
}
public interface IUnitOfWork : IDisposable
{
HiringManagerEntities DbContext { get; }
void Commit();
}
工人阶级的单位。
public class UnitOfWork : IUnitOfWork
{
private HiringManagerEntities dbContext;
public HiringManagerEntities DbContext
{
get
{
if (dbContext == null)
{
dbContext = new HiringManagerEntities();
dbContext.Database.CommandTimeout = 300;
}
return dbContext;
}
}
public void Dispose(bool disposing)
{
if (disposing)
{
if (dbContext != null)
{
dbContext.Dispose();
dbContext = null;
}
}
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
}
通用存储库:
public class GenericRepository<T> : IGenericRepository<T> where T : class
{
protected HiringManagerEntities DbContext;
private readonly IDbSet<T> dbSet;
public GenericRepository(IUnitOfWork unitOfWork)
{
this.DbContext = unitOfWork.DbContext;
dbSet = DbContext.Set<T>();
}
}
container.RegisterType<IUnitOfWork, UnitOfWork>(new HierarchicalLifetimeManager());
container.RegisterType<ISubCompanyService, SubCompanyService>();
在本地机器上,当我调试代码时,它从不产生任何错误或异常,但在生产过程中,日志上始终存在异常。
客户ID)
请向我建议任何可能的解决方法,并请让我知道是否应该从一开始就改变我的方法来解决这个问题。