0

First, I have 3 input text box (day, month, year). I made Json return from my controller and I made a key & value binding function with jquery. The problem is I don't know how to split Datetime value into day, month, year and bind to my inputs because that Datetime value is looks like

EntryDate: "/Date(940786200000)/"

My Controller

        [HttpGet]
        public ActionResult getBookById(int? id)
        {
            TB_EnglishBooks tbook = db.TB_EnglishBooks.Where(x => x.ItemID == id).FirstOrDefault();
            return Json(tbook, JsonRequestBehavior.AllowGet);
        }

My Html View

<input class="form-control" id="Day" type="text" placeholder="Day" style="width: 94px;">
<input class="form-control" id="Month" type="text" placeholder="Month" style="width: 94px;">
<input class="form-control" id="Year" type="text" placeholder="Year" style="width: 94px;">

My Binding Function

function bindcontrol(data) {
            $.each(data, function (key, value) {
                $("#mymodal").modal('show');
                if (value == true || value == false) {
                    $("#addform").find("input[type='checkbox'][name='" + key + "']").prop("checked", value);
                }
                else {
                    $("#addform").find("input[name='" + key + "']").val(value);
                    $("#addform").find("textarea[name='" + key + "']").val(value);
                    $("#addform").find("select[name='" + key + "']").val(value);
                }
            })
        }

1 Answers1

1

Please check this link

ASP.NET MVC JsonResult Date Format

Or if you want to perform server-side

https://www.developer.com/net/dealing-with-json-dates-in-asp.net-mvc.html

function ToJavaScriptDate(value) {
  var pattern = /Date\(([^)]+)\)/;
  var results = pattern.exec(value);
  var dt = new Date(parseFloat(results[1]));
  return (dt.getMonth() + 1) + "/" + dt.getDate() + "/" + dt.getFullYear();
}

function bindcontrol(data) 
{
    $.each(data, function (key, value) 
    {
        $("#mymodal").modal('show');
        if (value == true || value == false) {
            $("#addform").find("input[type='checkbox'][name='" + key + "']").prop("checked", value);
        }
        else 
        {
           if(key =="date") // here you date filed
           {
             $("#addform").find("input[name='" + key + "']").val(ToJavaScriptDate(value));
           }
           else
           {
               $("#addform").find("input[name='" + key + "']").val(value);
           }

            $("#addform").find("textarea[name='" + key + "']").val(value);
            $("#addform").find("select[name='" + key + "']").val(value);
        }
    });
}

If you want perform above operation server side need to add some class with JsonTextWriter

1) Create Json Result class

public class JsonNetResult : JsonResult
{
  public object Data { get; set; }

  public JsonNetResult()
  {
  }
  ...
}

2) Override method in controller level if you perform only controller level either you can create an attribute and register in global file as well as for all controller level.

public override void ExecuteResult(ControllerContext context)
{
  HttpResponseBase response = context.HttpContext.Response;
  response.ContentType = "application/json";
  if (ContentEncoding != null)
    response.ContentEncoding = ContentEncoding;
  if (Data != null)
  {
   JsonTextWriter writer = new JsonTextWriter(response.Output) { Formatting = Formatting.Indented };
   JsonSerializer serializer = JsonSerializer.Create(new JsonSerializerSettings());
   serializer.Serialize(writer, Data);
   writer.Flush();
  }
}

3) Controller Method

public JsonNetResult GetOrder(DateTime id)
{
   TB_EnglishBooks tbook = db.TB_EnglishBooks.Where(x => x.ItemID == id).FirstOrDefault();
            return Json(tbook, JsonRequestBehavior.AllowGet);
  return new JsonNetResult() { Data=tbook};
}
jishan siddique
  • 1,848
  • 2
  • 12
  • 23
  • Thanks. It's perfectly worked! It bind the datetime value to only one input which name is same with the key. But I want to know how can I split that datetime in 3 part (day, month, year) and bind to my day, month, year inputs. PS - I edited my Input tags. My input tags has no `name=""`. – Thu Htoo Aung Jan 20 '20 at 10:33
  • I got it! Thanks for your help! It's solved now! I really appreciate it! – Thu Htoo Aung Jan 20 '20 at 10:48
  • Glad to help you :) – jishan siddique Jan 20 '20 at 11:35