在我的模型中,我得到了一个DateTime(FechaNac)格式,我无法从json中获取它。
public class Persona
{
public int Id { get; set; }
[Display(Name = "Nombre")]
[Required]
public string Nombre { get; set; }
[Display(Name = "Apellido")]
[Required]
public string Apellido { get; set; }
[Display(Name = "Fecha de nacimiento")]
[Required]
[DataType(DataType.Date)]
public DateTime FechaNac { get; set; }
[Display(Name = "Lugar")]
[Required]
public int Idlugar { get; set; }
}
在控制器中,我获得了模型的大部分信息,
public JsonResult GetPostData(Persona jsonPersona)
{
Console.WriteLine(jsonPersona.ToString());
return Json(jsonPersona, "text/html", JsonRequestBehavior.AllowGet);
}
但是我无法得到日期,我在javascript中尝试了一种格式,但是没有成功。格式为ISO格式(来自datepicker)
@using MVCprueba2.Models
@model MVCprueba2.Models.Persona
<div class="form-group">
@Html.LabelFor(model => model.Nombre, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Nombre)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Apellido, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Apellido)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.FechaNac, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.JQueryUI().DatepickerFor(model => model.FechaNac)
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Idlugar, new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Idlugar)
</div>
</div>
<div id="mensaje">Mensaje</div>
<p><input type="submit" value="Save" id="guardarPersona" /></p>
<script type="text/javascript">
$(function () {
$("#guardarPersona").click(function () {
var persona = new Object();
persona.Nombre = $("#Nombre").val();
persona.Apellido = $("#Apellido").val();
persona.FechaNac = $('#FechaNac').datepicker("getDate"); //Solved [Typo]
persona.Idlugar = $("#Idlugar").val();
var jsonPersona = JSON.stringify(persona);
$.ajax({
url: "@Url.Action("GetPostData")",
type: "POST",
data: jsonPersona,
contentType: "application/json; charset=utf-8",
dataType: "json",
error: function (response) {
alert(response.responseText);
},
success: function (response) {
alert(response);
}
});
});
});
</script>
但是在控制器中,遗憾的是,我没有得到约会,我该如何解决?
答案 0 :(得分:0)
似乎按预期工作
在浏览器中
Fecha de nacimiento:29.10.2014
在控制器中
FechaNac = {29.10.2014 00:00:00}