我正在做一个Crud并且我遇到了麻烦,因为一些填充了.val()的字段不在帖子中。
如果填充是手动正常的话。
你能帮助我吗?
链接到ViewModel的视图。
@model CRUD.ViewModel.ClienteViewModel
@using (Html.BeginForm(null, null, FormMethod.Post,
new { name = "frm", id = "frm" }))
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<div class="form-group">
@Html.LabelFor(model => model.DscEndereco, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DscEndereco, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DscEndereco, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.NomBairro, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.NomBairro, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.NomBairro, "", new { @class = "text-danger",@id="valbairro" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Cep, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Cep, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Cep, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.CidadeId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CidadeId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CidadeId, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.UF, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UF, new { htmlAttributes = new { @class = "form-control" } })
@*@Html.ValidationMessageFor(model => model.UF, "", new { @class = "text-danger" })*@
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
<input type="hidden" id="hdnCep" value=""/>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
<script>
$('#Cep').on( "change",function (e) {
e.preventDefault();
var cep = $('#Cep').val().replace("-", "");
if ($('#hdnCep').val() == cep)
{
return false;
}
$("#DscEndereco").val('');
$("#NomBairro").val('');
$("#CidadeId").val('');
$("#Estado").val('');
$.getJSON("http://cep.republicavirtual.com.br/web_cep.php?cep=" + cep + "&formato=json", {}, function (data) {
if (data.resultado_txt = "sucesso - cep completo") {
$('#hdnCep').val(cep);
$('#DscEndereco').val(data.tipo_logradouro + ' ' + data.logradouro);
$('#NomBairro').val(data.bairro);
$('#CidadeId').val(data.cidade);
$('#UF').val(data.uf);
}
});
});
</script>
视图模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
namespace CRUD.ViewModel
{
public class ClienteViewModel
{
[Key]
public int ClienteId { get; set; }
[Required]
[Display(Name = "Endereço")]
public string DscEndereco { get; set; }
[Required]
[Display(Name = "Bairro")]
public string NomBairro { get; set; }
[Required]
[Display(Name = "CEP")]
public string Cep { get; set; }
[Required]
[Display(Name="Cidade")]
public string CidadeId { get; set; }
public virtual Crud.Models.Cidade Cidade { get; set; }
[Required]
public string UF { get; set; }
[Required]
public string Cnpj { get; set; }
}
}
控制器
// POST: Clientes/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see http://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create(ClienteViewModel clienteViewModel)
{
if (ModelState.IsValid)
{
}