5
I have a registration application in . net MVC and I am using Ajax to send the contents of the Forms For my Action that registers, when save the value of the fields, in my view the values are still visible in the fields not leaving blank ,as would be the default. Please check my code:
<div id="endereco">
@Html.Label("Endereco: ")
@Html.TextBoxFor(e => e.DescricaoEndereco, new { maxlength = "50", id = "Endereco", name = "Endereco" })
<br />
@Html.Label("Número: ")
@Html.TextBoxFor(e => e.Numero, new { maxlength = "50", id = "Numero" })
<br />
@Html.Label("Complemento: ")
@Html.TextBoxFor(e => e.Complemento, new { maxlength = "50", id = "Complemento" })
<br />
@Html.Label("Bairro: ")
@Html.TextBoxFor(e => e.Bairro, new { maxlength = "50", id = "Bairro", name = "Bairro" })
<br />
@Html.Label("Cidade: ")
@Html.TextBoxFor(e => e.Cidade, new { maxlength = "40", id = "Cidade", name = "Cidade" })
<br />
@Html.Label("UF: ")
@Html.DropDownListFor(e => e.UF, Model.UFList, new { id = "UF",name="UF" })
<br />
<input type="button" value="Adicionar Endereco" onclick="adicionarEndereco()"/>
<\div>
function adicionarEndereco() {
var codigoPessoa = $("#Pessoa").val();
var cep = $("#Cep").val();
var endereco = $("#Endereco").val();
var numero = $("#Numero").val();
var complemento = $("#Complemento").val();
var bairro = $("#Bairro").val();
var cidade = $("#Cidade").val();
//var temp = document.getElementById("UF");
var uf = $("#UF").val();
$.ajax(
{
type: "POST",
url: "/Master/CadastrarEndereco",
data: {
CodigoPessoa:codigoPessoa,
Cep: cep,
DescricaoEndereco:endereco,
Numero: numero,
Complemento: complemento,
Bairro: bairro,
Cidade: cidade,
UF : uf,
},
success: function (data) {
$("#endereco").html(data);
},
});
}
//Controller
[HttpPost]
public PartialViewResult CadastrarEndereco(SuperViewModel enderecoVM)
{
if (ModelState.IsValid)
{
var endereco = Extentions.MapearEndereco(enderecoVM);
EnderecoRepositorio.Cadastrar(endereco);
EnderecoRepositorio.Commit();
}
var superVM = new SuperViewModel();
superVM.UFList = Extentions.ObterUF();
superVM.Enderecos = EnderecoRepositorio.ObterEnderecoPorPessoa(enderecoVM.CodigoPessoa);
return PartialView("_EnderecoFields", superVM);
}
But what if he has one
select
ortextarea
also on the form?:input
would not work.– Wallace Maxters
@Wallacemaxters The selector
:input
selects all elements of the typesinput
,textarea
,select
andbutton
within the form. Documentation– André Ribeiro
Ah, yes! it’s true. It’s not
input
and yes:input
! Confudi! Deserved +1, Srsrsrsrsrsrs– Wallace Maxters