早上好,晚上或晚上
我想知道如何向 ModelState 中的现有键添加值。这是我所做的:
[HttpPost]
public ActionResult Create(VM_RoleGroup vm_roleGroup)
{
RoleGroup roleGroups = Mapper.Map<VM_RoleGroup, MAHAL_E_MA_Model.POCO.RoleGroup>(vm_roleGroup);
roleGroups.ModifiedDate = UtilityHands.GeneralTools.ConvertToShamsi(DateTime.Now);
roleGroups.Description = vm_roleGroup.Description ?? MAHAL_E_MA_Proj.ApplicationResources.GeneralResource.NoDescription;
**if (ModelState.ContainsKey("ModifiedDate") && ModelState["ModifiedDate"].Value == null)
{
ModelState["ModifiedDate"].Value = new ValueProviderResult("", roleGroups.ModifiedDate, CultureInfo.CurrentUICulture);
}
else if (!ModelState.ContainsKey("ModifiedDate"))
{
ModelState.Add("ModifiedDate", new ModelState { Value = new ValueProviderResult("", roleGroups.ModifiedDate, CultureInfo.CurrentUICulture) });
}**
if (ModelState.IsValid)
{
unitOfWork.RoleGroupRepository.InsertData(roleGroups);
unitOfWork.RoleGroupRepository.Save();
return RedirectToAction("Index");
}
return View(vm_roleGroup);
}
但是 ModelState 仍然无效,因为它缺少名为“ModifiedDate”的键的值。请记住,该值不是由用户在客户端设置的。它需要以编程方式设置。顺便说一句,“ModifiedDate”是数据库中的必填字段。
有什么想法吗?提前致谢