protected void lvImagensVeiculos_ItemCommand(object sender, ListViewCommandEventArgs e) { int codigo = int.Parse(e.CommandArgument.ToString()); imagens imagem = dc.imagens.Where(o => o.id == codigo).FirstOrDefault(); int veiculoid = Convert.ToInt32(imagem.veiculo_id); if (e.CommandName == "imagemDestaque") { if (imagem != null) { imagem.destaque = true; dc.SaveChanges(); } List <imagens> listaImagemVeiculo = dc.imagens.Where(o => o.id != codigo && o.veiculo_id == imagem.veiculo_id).ToList(); for (int i = 0; i < listaImagemVeiculo.Count(); i++) { listaImagemVeiculo[i].destaque = false; dc.SaveChanges(); } CarregarVeiculo(veiculoid); } else if (e.CommandName == "removerImagem") { dc.imagens.Remove(imagem); dc.SaveChanges(); CarregarVeiculo(veiculoid); } }
private void SetarImagemDestaque() { List <int> listaVeiculos = dc.veiculos.Select(s => s.id).ToList(); for (int i = 0; i < listaVeiculos.Count(); i++) { int carroid = listaVeiculos[i]; List <imagens> listaImagensVeiculo = dc.imagens.Where(o => o.veiculo_id == carroid).ToList(); bool carrotemDestaque = listaImagensVeiculo.Where(o => o.destaque == true).Any(); if (!carrotemDestaque && listaImagensVeiculo.Count() > 0) { imagens imagem = listaImagensVeiculo.First(); imagem.destaque = true; dc.SaveChanges(); } } }
protected void btnSalvar_Click(object sender, EventArgs e) { try { veiculos carro; string mensagem = "adicionado"; if (string.IsNullOrWhiteSpace(lblId.Text)) { carro = new veiculos(); } else { mensagem = "atualizado"; int codigoVeiculo = Convert.ToInt32(lblId.Text); carro = dc.veiculos.Where(o => o.id == codigoVeiculo).FirstOrDefault(); } carro.ano = txtAno.Text; carro.km = !string.IsNullOrWhiteSpace(txtKm.Text) ? Convert.ToInt32(txtKm.Text) : 0; carro.ativo = true; carro.cor = txtCor.Text; carro.data_criacao = DateTime.Now; carro.destaque = false; carro.modelo_id = Convert.ToInt32(ddlModelo.SelectedValue); carro.preco = Convert.ToDecimal(txtPreco.Text); carro.titulo = txtTitulo.Text; carro.descricao = txtDescricao.Text; if (ddlTipo.SelectedIndex > 0) { carro.tipo_id = Convert.ToInt32(ddlTipo.SelectedValue); } if (string.IsNullOrWhiteSpace(lblId.Text)) { dc.veiculos.Add(carro); } dc.SaveChanges(); string filepath = Server.MapPath("\\fotos"); HttpFileCollection uploadedFiles = Request.Files; Span1.Text = string.Empty; //if (string.IsNullOrWhiteSpace(lblId.Text)) //{ for (int i = 0; i < uploadedFiles.Count; i++) { HttpPostedFile userPostedFile = uploadedFiles[i]; try { if (userPostedFile.ContentLength > 0) { imagens imagem = new imagens(); imagens imagemExiste = dc.imagens.Where(o => o.arquivo == userPostedFile.FileName && o.veiculo_id == carro.id).FirstOrDefault(); if (imagemExiste == null) { imagem.arquivo = userPostedFile.FileName; imagem.destaque = false; imagem.veiculo_id = carro.id; dc.imagens.Add(imagem); dc.SaveChanges(); userPostedFile.SaveAs(filepath + "\\" + Path.GetFileName(userPostedFile.FileName)); } //Span1.Text += "<u>File #" + (i + 1) + "</u><br>"; //Span1.Text += "File Content Type: " + userPostedFile.ContentType + "<br>"; //Span1.Text += "File Size: " + userPostedFile.ContentLength + "kb<br>"; //Span1.Text += "File Name: " + userPostedFile.FileName + "<br>"; } } catch (Exception Ex) { Span1.Text += "Error: <br>" + Ex.Message; } } //} //else //{ //} Session["carroeditar"] = null; SetarImagemDestaque(); lvVeiculos.DataBind(); this.LimparCampos(this.Controls); ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "SUCESSO", "alert('Veículo " + mensagem + " com sucesso!');", true); } catch (Exception ex) { ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "ERRO", "alert('" + ex.Message + "');", true); } }