我正在用Post方法执行保存或编辑操作,我必须包括或编辑dropdow的值,但是我不知道该怎么做。
包括我已经在做的,但是当我编辑具有多选功能的下拉列表的值并删除该组件的数据时,将保留我已经拥有的那些,而不是删除并仅插入选定的内容以进行更新记录。
在api控制器中关注我的帖子代码
[JWTAuthFilter]
[System.Web.Http.HttpPost]
[System.Web.Http.Route("api/Register")]
public HttpResponseMessage Post([FromBody] UserDataModel userModel)
{
string pl = userModel.Name;
if (Regex.IsMatch(pl, @"^[a-zA-Z áéíóã]*$"))
{
var msg = "Nome válido";
}
else
{
var msg = "Nome inválido";
}
User user = new User();
if (userModel.Name == null || userModel.Name.Trim().Equals(""))
{
throw new Exception("ERRO");
}
if (userModel.IdUser != 0)
{
//editar -- edit
user = ctx.User.Find(userModel.IdUser);
}
else
{
//criar -- create
ctx.User.Add(user);
}
try
{
user.Area = ctx.Area.FirstOrDefault(it => it.IdArea == userModel.areasModel.Id);
user.ApplicantSubstituteUser = ctx.User.FirstOrDefault(it => it.IdUser == userModel.Applicant.apId);
/* comentário de teste */
//verificar
if (userModel.typesModel != null)
{
foreach (var typesModelScreen in userModel.typesModel)
{
user.RequestType.Add(ctx.RequestType.Single(it => it.IdRequestType == typesModelScreen.Id));
}
}
user.Name = userModel.Name;
user.IdTr = userModel.IdTr;
user.Password = userModel.Password;
user.Enabled = userModel.Enabled;
user.IsAdmin = userModel.IsAdmin;
user.IsRequester = userModel.IsRequester;
user.IsAnalyst = userModel.IsAnalyst;
user.Email = userModel.Email;
user.Manager = userModel.Manager;
user.ActingType = userModel.ActingType;
user.Telephone = userModel.Telephone;
user.DateRegister = DateTime.UtcNow;
ctx.SaveChanges();
var message = Request.CreateResponse(HttpStatusCode.Created, userModel + "Usuário registrado com sucesso!");
return message;
}
catch (Exception ex)
{
return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex);
}
}