asp.net mvc 5 - Trying to send datetime to controller via json -
asp.net mvc 5 - Trying to send datetime to controller via json -
in model got datetime (fechanac) format can't 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; } }
in controller of info of model,
public jsonresult getpostdata(persona jsonpersona) { console.writeline(jsonpersona.tostring()); homecoming json(jsonpersona, "text/html", jsonrequestbehavior.allowget); }
but can't date, tried format in javascript in different ways no success. format in iso format (from 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>
but in controller, sadly, don't date, how can prepare ?
seems working expected
in browser
fecha de nacimiento: 29.10.2014
in controller
fechanac = {29.10.2014 00:00:00}
asp.net-mvc-5
Comments
Post a Comment