我有两个控制器为SaveOrUpdate调用相同的CommandHandler。其中一个Controller是普通的MVC 控制器,而另一个是WebAPI ApiController 。两者在Save中工作正常但在更新时它只适用于普通的MVC控制器,不适用于 ApiController 。
我检查了来自ApiController的ViewModel,其填充与普通Controller完全相同。我还对数据库进行了一些跟踪,似乎它没有运行Update Query,只是忽略了提交的数据。
这是代码
public class SaveOrUpdateRequestNameChangeCommandHandler : ICommandHandler<SaveOrUpdateRequestNameChangeCommand>
{
private readonly INHibernateRepository<RequestStatus> requestStatusRepository;
private readonly INHibernateRepository<RequestNameChange> requestNameChangeRepository;
public SaveOrUpdateRequestNameChangeCommandHandler(
INHibernateRepository<RequestStatus> requestStatusRepository,
INHibernateRepository<RequestNameChange> requestNameChangeRepository)
{
this.requestStatusRepository = requestStatusRepository;
this.requestNameChangeRepository = requestNameChangeRepository;
}
public void Handle(SaveOrUpdateRequestNameChangeCommand command)
{
var requestNameChange = command.RequestNameChangeId.HasValue
? requestNameChangeRepository.Get(command.RequestNameChangeId.Value)
: new RequestNameChange();
requestNameChange.Username = command.Username;
requestNameChange.OriginalFirstName = command.OriginalFirstName;
requestNameChange.OriginalLastName = command.OriginalLastName;
requestNameChange.NewFirstName = command.NewFirstName;
requestNameChange.NewLastName = command.NewLastName;
requestNameChange.EffectiveDate = command.EffectiveDate;
requestNameChange.NewEmailAddress = command.NewEmailAddress;
requestNameChange.IsRetailUser = command.IsRetailUser;
requestNameChange.SpecialRequirements = command.SpecialRequirements;
requestNameChange.Notes = command.Notes;
requestNameChange.ServiceDeskId = command.ServiceDeskId;
requestNameChange.ServiceDeskUrl = command.ServiceDeskUrl;
if (command.RequestStatusId != null)
requestNameChange.RequestStatus = requestStatusRepository.Get(command.RequestStatusId.Value);
requestNameChange.DateCreated = command.DateCreated;
requestNameChange.CreatedBy = command.CreatedBy;
requestNameChangeRepository.SaveOrUpdate(requestNameChange);
command.RequestNameChangeId = requestNameChange.Id;
}
}
我不确定我做错了什么,这让我在过去的几天里发疯了。
以下是来自普通控制器
的值
这是来自 ApiController
的值
正如你所看到的,除了CreatedBy之外它们完全相同(不要担心这个,我确实在我的测试中使这个相同)但是它们来自ApiController的一个不会更新值。
非常感谢任何帮助。
---------更新---------
以下是控制器
的代码[HttpPost]
[Transaction]
public ActionResult SaveOrUpdate(RequestNameChangesViewModel viewModel, [DataSourceRequest]DataSourceRequest request)
{
if (ModelState.IsValid)
{
var command = new SaveOrUpdateRequestNameChangeCommand(
viewModel.RequestNameChangeId,
viewModel.Username,
viewModel.OriginalFirstName,
viewModel.OriginalLastName,
viewModel.NewFirstName,
viewModel.NewLastName,
viewModel.EffectiveDate,
viewModel.NewEmailAddress,
viewModel.IsRetailUser,
viewModel.SpecialRequirements,
viewModel.Notes,
viewModel.ServiceDeskId,
viewModel.ServiceDeskUrl,
viewModel.RequestStatusId,
viewModel.DateCreated,
viewModel.CreatedBy
);
if (ModelState.IsValid)
{
commandProcessor.Process(command);
viewModel.RequestNameChangeId = command.RequestNameChangeId;
}
}
var result = new[] { viewModel }.ToDataSourceResult(request, ModelState);
return Json(result);
}
这是ApiController的代码
// POST Api/<controller>
[Transaction]
public void Post(RequestNameChangesViewModel viewModel)
{
if (ModelState.IsValid)
{
var command = new SaveOrUpdateRequestNameChangeCommand(
viewModel.RequestNameChangeId,
viewModel.Username,
viewModel.OriginalFirstName,
viewModel.OriginalLastName,
viewModel.NewFirstName,
viewModel.NewLastName,
viewModel.EffectiveDate,
viewModel.NewEmailAddress,
viewModel.IsRetailUser,
viewModel.SpecialRequirements,
viewModel.Notes,
viewModel.ServiceDeskId,
viewModel.ServiceDeskUrl,
viewModel.RequestNameChangeId,
viewModel.DateCreated,
viewModel.CreatedBy
);
if (ModelState.IsValid)
{
commandProcessor.Process(command);
viewModel.RequestNameChangeId = command.RequestNameChangeId;
}
}
}
// PUT Api/<controller>/5
[Transaction]
public void Put(int id, RequestNameChangesViewModel viewModel)
{
if (ModelState.IsValid)
{
var command = new SaveOrUpdateRequestNameChangeCommand(
viewModel.RequestNameChangeId,
viewModel.Username,
viewModel.OriginalFirstName,
viewModel.OriginalLastName,
viewModel.NewFirstName,
viewModel.NewLastName,
viewModel.EffectiveDate,
viewModel.NewEmailAddress,
viewModel.IsRetailUser,
viewModel.SpecialRequirements,
viewModel.Notes,
viewModel.ServiceDeskId,
viewModel.ServiceDeskUrl,
viewModel.RequestNameChangeId,
viewModel.DateCreated,
viewModel.CreatedBy
);
if (ModelState.IsValid)
{
commandProcessor.Process(command);
viewModel.RequestNameChangeId = command.RequestNameChangeId;
}
}
}
答案 0 :(得分:1)
在我看来,在API方法中没有使用id字段,不应该将它设置为命令对象吗?
// PUT Api/<controller>/5
[Transaction]
public void Put(int id, RequestNameChangesViewModel viewModel)
{
if (ModelState.IsValid)
{
var command = new SaveOrUpdateRequestNameChangeCommand(
id,
viewModel.Username,
viewModel.OriginalFirstName,
viewModel.OriginalLastName,
viewModel.NewFirstName,
viewModel.NewLastName,
viewModel.EffectiveDate,
viewModel.NewEmailAddress,
viewModel.IsRetailUser,
viewModel.SpecialRequirements,
viewModel.Notes,
viewModel.ServiceDeskId,
viewModel.ServiceDeskUrl,
viewModel.RequestNameChangeId,
viewModel.DateCreated,
viewModel.CreatedBy
);
if (ModelState.IsValid)
{
commandProcessor.Process(command);
viewModel.RequestNameChangeId = command.RequestNameChangeId;
}
}
}
答案 1 :(得分:1)
之后
requestNameChangeRepository.SaveOrUpdate(requestNameChange);
只需提交更改即可添加此代码。
requestNameChangeRepository.DbContext.CommitChanges();